Dropbox Authentication¶
Description:
- Authenticate with Dropbox to interact with data in your dropbox account
Resources:
- [Generate an Access Token] https://dropbox.tech/developers/generate-an-access-token-for-your-own-account
Environment variables¶
This notebook reads its settings from environment variables. Set them before starting Jupyter:
DROPBOX_APP_KEY: theApp keyfrom your Dropbox app'sSettingspage
Example 1¶
Set up the Dropbox client with a refresh token
In [ ]:
Copied!
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)
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)
Example 2¶
Set up the Dropbox client with a short-lived access token only
In [ ]:
Copied!
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)
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)