Laravel Dropbox API

A Laravel package for working with Dropbox API.

https://github.com/daveismyname/laravel-dropbox

Dropbox API documentation can be found at:
https://www.dropbox.com/developers/documentation/http/documentation

Application Register

To use Dropbox API an application needs creating at https://www.dropbox.com/developers/apps

Create a new application, select either Dropbox API or Dropbox Business API
Next select type of access needed either app folder (useful for isolating to a single folder), or full Dropbox.

Next copy and paste the APP Key and App Secret into your .env file:


DROPBOX_CLIENT_ID=
DROPBOX_SECRET_ID=

Now enter you desired redirect url. This is the url your application will use to connect to Dropbox API.

A comman URL is https://domain.com/dropbox/oauth

Install

Via Composer

composer require daveismyname/laravel-dropbox

In Laravel 5.5 the service provider will automatically get registered. In older versions of the framework just add the service provider in config/app.php file


'providers' => [
    // ...
    Daveismyname\Dropbox\DropboxServiceProvider::class,
];

Config

You can publish the config file with:

php artisan vendor:publish --provider="Daveismyname\Dropbox\DropboxServiceProvider" --tag="config"

When published, the config/dropbox.php config file contains:


return [

    /*
    * set the client id
    */
    'clientId' => env('DROPBOX_CLIENT_ID'),

    /*
    * set the client secret
    */
    'clientSecret' => env('DROPBOX_SECRET'),

    /*
    * Set the url to trigger the oauth process this url should call return MsGraph::connect();
    */
    'redirectUri' => env('DROPBOX_OAUTH_URL'),

    /*
    * Set the url to redirecto once authenticated;
    */
    'landingUri' => env('DROPBOX_LANDING_URL', '/'),

    /**
     * Set access token, when set will bypass the oauth2 process
     */
    'accessToken' => env('DROPBOX_ACCESS_TOKEN', ''),
];

Migration

You can publish the migration with:


php artisan vendor:publish --provider="Daveismyname\Dropbox\DropboxServiceProvider" --tag="migrations"

After the migration has been published you can create the tokens tables by running the migration:


php artisan migrate

.ENV Configuration

Ensure you've set the following in your .env file:


DROPBOX_CLIENT_ID=
DROPBOX_SECRET_ID=
DROPBOX_OAUTH_URL=https://domain.com/dropbox/oauth
DROPBOX_LANDING_URL=https://domain.com/dropbox

Bypass Oauth2

You can bypass the oauth2 process by generating an access token in your dropbox app and entering it on the .env file:


DROPBOX_ACCESS_TOKEN=

Usage

Note this package expects a user to be logged in.

Note: these examples assume the authentication is using the oauth2 and not setting the access toekn in the .env directly.

If setting the access code directly don't rely on Dropbox::getAccessToken()

A routes example:


Route::group(['middleware' => ['web', 'auth']], function(){
    Route::get('dropbox', function(){

        if (! is_string(Dropbox::getAccessToken())) {
            return redirect(env('DROPBOX_OAUTH_URL'));
        } else {
            //display your details
            return Dropbox::get('users/get_current_account');
        }

    });

    Route::get('dropbox/oauth', function(){
        return Dropbox::connect();
    });
});

Or using a middleware route, if user does not have a graph token then automatically redirect to get authenticated:


Route::group(['middleware' => ['web', 'DropboxAuthenticated']], function(){
    Route::get('dropbox', function(){
        return Dropbox::get('users/get_current_account');
    });
});

Route::get('dropbox/oauth', function(){
    return Dropbox::connect();
});

Once authenticated you can call Dropbox:: with the following verbs:


Dropbox::get($endpoint, $array = [], $headers = [], $useToken = true)
Dropbox::post($endpoint, $array = [], $headers = [], $useToken = true)
Dropbox::put($endpoint, $array = [], $headers = [], $useToken = true)
Dropbox::patch($endpoint, $array = [], $headers = [], $useToken = true)
Dropbox::delete($endpoint, $array = [], $headers = [], $useToken = true)

The $array is not always required, its requirement is determined from the endpoint being called, see the API documentation for more details.

The $headers is optional when used can pass in additional headers.

The $useToken is optional when set to true will use the authorisation header, defaults to true.

These expect the API endpoints to be passed, the url https://api.dropboxapi.com/2/ is provided, only endpoints after this should be used ie:


Dropbox::get('users/get_current_account')

Middleware

To restrict access to routes only to authenticated users there is a middleware route called DropboxAuthenticated

Add DropboxAuthenticated to routes to ensure the user is authenticated:


Route::group(['middleware' => ['web', 'DropboxAuthenticated'], function()

To access token model reference this ORM model:


use Daveismyname\Dropbox\Models\DropboxToken;

Working with Files

This package provides a clean way of working with files.

To work with files first call ->files() followed by a method.

Import Namespace


use Daveismyname\Dropbox\Facades\Dropbox;

List Content

list files and folders of a given path


Dropbox::files()->listContents($path = '')

List Content Continue

Using a cursor from the previous listContents call to paginate over the next set of folders/files.


Dropbox::files()->listContentsContinue($cursor = '')

Delete folder/file

Pass the path to the file/folder, When delting a folder all child items will be deleted.


Dropbox::files()->delete($path)

Create Folder

Pass the path to the folder to be created.


Dropbox::files()->createFolder($path)

Search Files

Each word will used to search for files.


Dropbox::files()->search($query)

Upload File

Upload files to Dropbox by passing the folder path followed by the filename. Note this method supports uploads up to 150MB only.


Dropbox::files()->upload($path, $file)

Download File

Download file from Dropbox by passing the folder path including the file.


Dropbox::files()->download($path)