quadrogod / dropbox-oauth-manager
Simple Dropbox OAuth Manager
Requires
- php: ^7.2 || ^8.0
- ext-json: *
- lib-curl: *
README
This is a simple PHP package for authenticating with the Dropbox OAuth service to obtain access_token
, refresh_token
, and automatically refresh the access_token
when a valid refresh_token
is present.
Install
composer require quadrogod/dropbox-oauth-manager
Usage
use Quadrogod\DropboxManager\DropboxOAuthManager; $config = [ 'client_id' => '', // App key 'client_secret' => '', // App secret 'redirect_uri' => '', // Your redirect URI (must be set in Dropbox App settings) 'token_file' => __DIR__ . "/token.json", // Path to save token data ]; $dropboxManager = new DropboxOAuthManager($config); $dropboxManager->renderLoginPage();
Once you receive the CODE
from Dropbox:
$dropboxManager->getAndSaveToken($code);
This will save the token data to the default $config['token_file']
path.
You can also use callback functions to handle and save token data according to your own logic:
$dropboxManager->getAndSaveToken($code, function ($token, $response) { var_dump($token, $response); }, function ($response) { var_dump($response); });
More examples can be found in the
example
folder.
Example Project
The example/
folder contains a fully working demo of how to use this package.
To simplify testing, a Docker
environment is included. To start the example:
cd example && docker-compose up -d
or
docker compose up -d
Now you need to enter the created container and install the dependencies.
docker ps
docker exec -it example_php_1 bash
composer install
Then open http://localhost in your browser.
You must first fill in the file example/config.env.php
with your own app credentials.
To create a Dropbox App, visit the Dropbox Developer Console and create a new app.
Don’t forget to enable the necessary permissions for your app.
⏱ Cron Task
Dropbox now issues short-lived access_token
s valid for 4 hours to enhance account security.
You must automatically refresh the token before it expires.
The recommended way is to configure a cron
job that runs hourly:
0 * * * * php cron.php >/dev/null 2>&1
This will ensure your token is always up to date.
Note: Currently, the token data is loaded from the default file. Passing external
$tokenData
is not yet supported but will be added in future releases.
🧭 Roadmap
- Refactor and improve the codebase
- Make
DropboxOAuthManager
more universal and reusable - Improve token management logic
- Add better exception handling and logging
☕ Support Me
If you like this project and want to support further development:
https://buymeacoffee.com/quadrogod
MIT License © Alex Molchanov