spatie / flysystem-dropbox

A flysystem driver for Dropbox that uses the v2 API
https://freek.dev/734-dropbox-will-turn-off-v1-of-their-api-soon-its-time-to-update-your-php-application
MIT License
342 stars 50 forks source link

Dropbox Refresh token needed - Long Lived Access Tokens Depecrated #95

Closed mho22 closed 9 months ago

mho22 commented 1 year ago

Prior to : #86

I wanted to ask you if a new PR was necessary now that Dropbox has an updated way to work with tokens. It needs to generate a Refresh token beforehand. I added my way of working in the issue 86.

Since mid 2021, it is now mandatory to use the dropbox refresh token process :

In the past, the Dropbox API used only long-lived access tokens. 
These are now deprecated, but will remain available as an option in the Developer console for compatibility until mid 2021.

I tried something else personally, without using AutoRefreshingDropBoxTokenService [ not necessary when using the flysystem-dropbox ] and it is less verbose :

You will still need to authorize the access to the Dropbox App using this link :

https://www.dropbox.com/oauth2/authorize?client_id<YOUR_APP_KEY>&response_type=code&token_access_type=offline

This will give you the authorization_code needed to retrieve a refresh_token with this curl request :

curl https://api.dropbox.com/oauth2/token -d code=<ACCESS_CODE> -d grant_type=authorization_code -u <APP_KEY>:<APP_SECRET>

Giving you access to the refresh_token needed in the DROPBOX_REFRESH_TOKEN indicated below . The other elements are available in your Dropbox App.

.env.example

DROPBOX_APP_KEY=
DROPBOX_APP_SECRET=
DROPBOX_REFRESH_TOKEN=
DROPBOX_TOKEN_URL=https://${DROPBOX_APP_KEY}:${DROPBOX_APP_SECRET}@api.dropbox.com/oauth2/token

config/filesystems.php


        'dropbox' => [
            'driver' => 'dropbox',
            'token_url' => env('DROPBOX_TOKEN_URL'),
            'refresh_token' => env('DROPBOX_REFRESH_TOKEN'),
        ],

app/Providers/DropboxServiceProvider.php

use Illuminate\Contracts\Foundation\Application;
use Illuminate\Filesystem\FilesystemAdapter;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\ServiceProvider;
use League\Flysystem\Filesystem;
use Spatie\Dropbox\Client as DropboxClient;
use Spatie\FlysystemDropbox\DropboxAdapter;
use GuzzleHttp\Client;

class DropboxServiceProvider extends ServiceProvider
{
    public function boot(): void
    {
        Storage::extend( 'dropbox', function( Application $app, array $config )
        {
            $resource = ( new Client() )->post( $config[ 'token_url' ] , [
                    'form_params' => [ 
                            'grant_type' => 'refresh_token', 
                            'refresh_token' => $config[ 'refresh_token' ] 
                    ] 
            ]);

            $accessToken = json_decode( $resource->getBody(), true )[ 'access_token' ];

            $adapter = new DropboxAdapter( new DropboxClient( $accessToken ) );

            return new FilesystemAdapter( new Filesystem( $adapter, $config ), $adapter, $config );
        });
    }
}

A PR in code and documentation should be interesting. Should I PR this ? The previous code is also indicated in the Laravel 10 Documentation - Custom Filesystems

spatie-bot commented 9 months ago

Dear contributor,

because this issue seems to be inactive for quite some time now, I've automatically closed it. If you feel this issue deserves some attention from my human colleagues feel free to reopen it.

mho22 commented 9 months ago

I think this issue / PR should be reopened.

damms005 commented 3 months ago

@freekmurze should not tag you here, but I think you may want to address this yourself, sir