Dropbox¶
A Django files storage using Dropbox as a backend via the official Dropbox SDK for Python. Currently only v2 of the API is supported.
Before you start configuration, you will need to install the SDK which can be done for you automatically by doing:
pip install django-storages[dropbox]
Settings¶
To use DropboxStorage set:
# django < 4.2
DEFAULT_FILE_STORAGE = "storages.backends.dropbox.DropboxStorage"
# django >= 4.2
STORAGES = {"default": {"BACKEND": "storages.backends.dropbox.DropboxStorage"}}
Two methods of authenticating are supported:
- using an access token
- using a refresh token with an app key and secret
Dropbox has recently introduced short-lived access tokens only, and does not seem to allow new apps to generate access tokens that do not expire. Short-lived access tokens can be indentified by their prefix (short-lived access tokens start with 'sl.'
).
Please set the following variables accordingly:
DROPBOX_OAUTH2_TOKEN
- Your Dropbox token. You can obtain one by following the instructions in the tutorial.
DROPBOX_APP_KEY
- Your Dropbox appkey. You can obtain one by following the instructions in the tutorial.
DROPBOX_APP_SECRET
- Your Dropbox secret. You can obtain one by following the instructions in the tutorial.
DROPBOX_OAUTH2_REFRESH_TOKEN
- Your Dropbox refresh token. You can obtain one by following the instructions in the tutorial.
The refresh token can be obtained using the commandline-oauth.py example from the Dropbox SDK for Python.
DROPBOX_ROOT_PATH
(optional, default"/"
)- Path which will prefix all uploaded files. Must begin with a
/
. DROPBOX_TIMEOUT
(optional, default100
)- Timeout in seconds for requests to the API. If
None
, the client will wait forever. The default value matches the SDK at the time of this writing. DROPBOX_WRITE_MODE
(optional, default"add"
)- Sets the Dropbox WriteMode strategy. Read more in the official docs.
Obtain the refresh token manually¶
You can obtail the refresh token manually via APP_KEY
and APP_SECRET
.
Obtain the refresh token¶
Usinh your APP_KEY
, APP_SECRET
and AUTHORIZATION_KEY
obtain the refresh token.
curl -u APP_KEY:APP_SECRET \
-d "code=AUTHORIZATION_CODE&grant_type=authorization_code" \
-H "Content-Type: application/x-www-form-urlencoded" \
-X POST "https://api.dropboxapi.com/oauth2/token"
The response would be:
{
"access_token": "sl.************************",
"token_type": "bearer",
"expires_in": 14400,
"refresh_token": "************************", <-- your REFRESH_TOKEN
"scope": <SCOPES>,
"uid": "************************",
"account_id": "dbid:************************"
}