Hi folks,
I have been working on a Python script to read information from my Dropbox account. The Dropbox OAuth SDK examples only demonstrate the initial part of the flow and do not use the refresh token. I could not find a working example despite searching the Internet (and this forum).
After a bit of trial-and-error, I have a posted working example on GitHub: Dropbox Command-line OAuth with Refresh.
Once the user has authorized Dropbox access, the script saves the resulting refresh token in .env. Subsequent executions of the script do not require any user intervention -- the refresh token is used instead.
The key bits I learned along the way:
1. This is sufficient to obtain an initial refresh token:
flow = DropboxOAuth2FlowNoRedirect(
APP_KEY,
APP_SECRET,
token_access_type="offline"
)2. Once the refresh token has been obtained, this is sufficient to access Dropbox:
with dropbox.Dropbox(
app_key=APP_KEY,
app_secret=APP_SECRET,
oauth2_refresh_token=refresh_token
) as dbx:3. There is no need to use the access token.