Oh Dear is the all-in-one monitoring tool for your entire website. We monitor uptime, SSL certificates, broken links, scheduled tasks and more. You'll get a notifications for us when something's wrong. All that paired with a developer friendly API and kick-ass documentation. O, and you'll also be able to create a public status page under a minute. Start monitoring using our free trial now.

Dropbox will turn off v1 of their API soon. It's time to update your PHP application.

Original – by Freek Van der Herten – 4 minute read

Last year on 28th of June Dropbox deprecated v1 of their API. On the same date this year they will turn off all v1 endpoints. If you're using Flysystem, Laravel or the official PHP SDK to work with Dropbox, it's the time to update.

Last week my company released a Dropbox API client and a Flysystem Dropbox adapter that both use v2 of the dropbox API. In this blogpost I'd like to explain how to install and use these packages.

Updating Flysytem adapters

Flysystem is an awesome package created by Frank de Jonge that abstracts filesystems. It provides a common API for all kinds of filesystems. So you can use the same API to work with S3, Dropbox, Sftp servers and whatnot. Until recently Flysystem suggested to use their home cooked Dropbox adapter. That driver only supports v1 of the Dropbox API. Therefore will stop working after 28th of June of this year. If your project uses that adapter you can very easily swap it with our new Dropbox adapter.

This is how you newed up the old adapter:

use League\Flysystem\Dropbox\DropboxAdapter;
use League\Flysystem\Filesystem;
use Dropbox\Client;

$client = new Client($accessToken, $appSecret);
$adapter = new DropboxAdapter($client);

$filesystem = new Filesystem($adapter);

This is how you should new up the new adapter (after having run composer require spatie/flysystem-dropbox.

use League\Flysystem\Filesystem;
use Spatie\Dropbox\Client;
use Spatie\FlysystemDropbox\DropboxAdapter;

$client = new Client($authorizationToken);

$adapter = new DropboxAdapter($client);

$filesystem = new Filesystem($adapter);

Notice that instead of an accessToken and an appSecret you now need a authorizationToken. Luckily Dropbox has made this very easy. You can just generate a token in the App Console for any Dropbox API app. You'll find more info at the Dropbox Developer Blog.

Updating Laravel applications

Laravel comes with filesystem abstraction out of the box. Under the hood it's powered by Flysystem. So if you're currently using a Dropbox disk in Laravel you also have a bit of work to upgrade. In order to make use of Dropbox disk you had to create a service provider not unlike this one (example taken from the old Laravel docs):

namespace App\Providers;

use Storage;
use League\Flysystem\Filesystem;
use Dropbox\Client as DropboxClient;
use Illuminate\Support\ServiceProvider;
use League\Flysystem\Dropbox\DropboxAdapter;

class DropboxServiceProvider extends ServiceProvider
{
    /**
     * Perform post-registration booting of services.
     *
     * @return void
     */
    public function boot()
    {
        Storage::extend('dropbox', function ($app, $config) {
            $client = new DropboxClient(
                $config['accessToken'], $config['clientIdentifier']
            );

            return new Filesystem(new DropboxAdapter($client));
        });
    }

    /**
     * Register bindings in the container.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

To upgrade simply pull in our Dropbox adapter by running composer require spatie/flysystem-dropbox. Next you must update your service provider.

namespace App\Providers;

use Storage;
use League\Flysystem\Filesystem;
use Spatie\Dropbox\Client as DropboxClient;
use Illuminate\Support\ServiceProvider;
use Spatie\FlysystemDropbox\DropboxAdapter;

class DropboxServiceProvider extends ServiceProvider
{
    /**
     * Perform post-registration booting of services.
     *
     * @return void
     */
    public function boot()
    {
        Storage::extend('dropbox', function ($app, $config) {
            $client = new DropboxClient(
                $config['authorizationToken']
            );

            return new Filesystem(new DropboxAdapter($client));
        });
    }

    /**
     * Register bindings in the container.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

Like mentioned above you can generate authorization token on the App Console at Dropbox.

And with your Laravel application makes use of Dropbox API v2.

Updating other applications

If your application uses the old Dropbox PHP SDK you'll be sad to learn that there currently are no plans to release and official PHP SDK for v2.

But you have a few options to upgrade. You could opt to use our dropbox-api package. It currently only support the methods needed by our Flysystem dropbox adapter, but we're open for PRs that add more methods to the package.

Alternativly you could use the Dropbox package by Kunal Varma or the Dropbox SDK made by Alorel, but both don't have stable releases just yet.

In closing

If you're using Dropbox in your application be sure to verify that you're using v2 of the API. Still on v1? Then be sure to upgrade before 28th of July to avoid tears or worse. Our Flysystem adapter and API package can help out.

These packages are not the first ones my company has released. Be sure to check out this pile of packages we've previously released.

Stay up to date with all things Laravel, PHP, and JavaScript.

You can follow me on these platforms:

On all these platforms, regularly share programming tips, and what I myself have learned in ongoing projects.

Every month I send out a newsletter containing lots of interesting stuff for the modern PHP developer.

Expect quick tips & tricks, interesting tutorials, opinions and packages. Because I work with Laravel every day there is an emphasis on that framework.

Rest assured that I will only use your email address to send you the newsletter and will not use it for any other purposes.

Comments

What are your thoughts on "Dropbox will turn off v1 of their API soon. It's time to update your PHP application."?

Comments powered by Laravel Comments
Want to join the conversation? Log in or create an account to post a comment.