Skip to content

Description:

  • Authenticate with Dropbox to interact with data in your dropbox account

Resources:

This notebook reads its settings from environment variables. Set them before starting Jupyter:

  • DROPBOX_APP_KEY: the App key from your Dropbox app’s Settings page

Set up the Dropbox client with a refresh token

import os
import dropbox
from dropbox import DropboxOAuth2FlowNoRedirect
APP_KEY = os.environ["DROPBOX_APP_KEY"]
# PKCE lets an app without a server-side secret (such as a notebook) authorize itself.
# token_access_type="offline" asks Dropbox for a refresh token as well as an access token.
flow = DropboxOAuth2FlowNoRedirect(APP_KEY, use_pkce=True, token_access_type="offline")
print("Open this page, allow the app, and copy the code that Dropbox shows:")
print(flow.start())
result = flow.finish(input("Authorization code: ").strip())
# With a refresh token and the app key, the client renews its access token by itself.
with dropbox.Dropbox(oauth2_refresh_token=result.refresh_token, app_key=APP_KEY) as dbx:
account = dbx.users_get_current_account()
print("client ready for account", account.account_id)

Set up the Dropbox client with a short-lived access token only

import os
import dropbox
from dropbox import DropboxOAuth2FlowNoRedirect
APP_KEY = os.environ["DROPBOX_APP_KEY"]
# token_access_type="online" returns only an access token. It expires after a few hours,
# after which you run this flow again (or use the refresh-token flow above).
flow = DropboxOAuth2FlowNoRedirect(APP_KEY, use_pkce=True, token_access_type="online")
print("Open this page, allow the app, and copy the code that Dropbox shows:")
print(flow.start())
result = flow.finish(input("Authorization code: ").strip())
print("access token expires at", result.expires_at)
with dropbox.Dropbox(oauth2_access_token=result.access_token) as dbx:
account = dbx.users_get_current_account()
print("client ready for account", account.account_id)

ganymede is made by Eugo