> Every page of the ganymede website is listed in https://eugo.io/docs/ganymede/llms.txt.

# Dropbox Authentication

> Dropbox Authentication: Authenticate with Dropbox to interact with data in your dropbox account. A Python demo notebook from ganymede.

Web page: https://eugo.io/docs/ganymede/demo_notebooks/data_storage/filesystems/dropbox/authentication/
Notebook: https://eugo.io/docs/ganymede/demo_notebooks/data_storage/filesystems/dropbox/authentication/authentication.ipynb (source: https://github.com/eugo-inc/ganymede/blob/main/demo_notebooks/data_storage/filesystems/dropbox/authentication.ipynb)

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`: the `App key` from your Dropbox app's `Settings` page

## Example 1

Set up the Dropbox client with a refresh token

```python
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

```python
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)
```
