Python Dropbox command-line example using OAuth with a refresh token.
Dropbox SDK examples show how to handle the start of the OAuth flow, prompting the user to click a URL and authorize the app.
I was not able to find examples using the refresh token.
- go to the Dropbox App Console.
- click 'create an app'
- choose a scoped access API
- choose the type of access you need: app folder or full dropbox
- name your app
This results in two secrets:
- an app key
- an app secret
Store those in a .env file. Because they are secret, do not add them to git, keep them read-write only for the user.
-
the first time you run the script, it will prompt you to seek an authorization code:
./oauth2-with-refresh.py oauth2-with-refresh.py - INFO - Initiating OAuth flow.
- Go to: https://www.dropbox.com/oauth2/authorize?response_type=code&client_id=SOME_ID&token_access_type=offline
- Click 'Allow' (you might have to log into Dropbox first).
- Copy the authorization code.
Enter the authorization code here:
-
After pasting the authorization code, the script connects to Dropbox and reads the display name of the account:
oauth2-with-refresh.py - INFO - Stored new refresh token
Dropbox display name: Don Murray
-
The refresh token is now stored in .env, and can be used without re-authorizing:
./oauth2-with-refresh.py
oauth2-with-refresh.py - INFO - We've received a refresh token in the past
oauth2-with-refresh.py - INFO - Using our existing refresh token
Dropbox display name: Don Murray