Dropbox OAuth 2 helpers, built on hot.dev/oauth2: the authorization
code flow with PKCE and token_access_type=offline (so Dropbox
returns a long-lived refresh token), code exchange, refresh and
revocation.
Flow
- Redirect the user to
authorize-url(...)(offline access by default). Keepstateand the PKCE verifier for the callback. - In the callback,
exchange-code(...)turns?code=into tokens:access_token(about 4 hours),refresh_token,expires_in,scope,account_id,uid, andteam_idfor team apps. - Store
refresh_tokenper user, then pass it per call as{refresh-token, app-key, app-secret}opts, or set thedropbox.refresh.token/dropbox.app.key/dropbox.app.secretcontext variables for a single-user app.
::oauth ::dropbox::oauth
verifier ::oauth2/pkce-verifier()
url ::oauth/authorize-url("abc123appkey", "https://app.example.com/oauth/dropbox", "csrf-123", {
code-verifier: verifier,
scope: ["files.metadata.read", "files.content.read"]
})
// callback handler:
tokens ::oauth/exchange-code("abc123appkey", null, "code-from-query",
"https://app.example.com/oauth/dropbox", {code-verifier: verifier})
tokens.refresh_token
Values
REVOKE_PATH
REVOKE_PATH: Str "/auth/token/revoke"
Functions
authorize-url
fn (app-key: Str, redirect-uri: Str?, state: Str?): Str
fn (app-key: Str, redirect-uri: Str?, state: Str?, opts: Map): Str
Build the Dropbox consent URL
(https://www.dropbox.com/oauth2/authorize). app-key is the App
Console key; redirect-uri must be registered there (null for the
no-redirect flow, where Dropbox shows the code to the user); state
is your CSRF token. See build-authorize-params for opts.
url ::dropbox::oauth/authorize-url("abc123appkey", "https://app.example.com/oauth/dropbox", "csrf-123")
bool-param
fn (value: Any): Str?
A Bool option as a query value: "true", "false", or null when unset.
Example
::dropbox::oauth/bool-param(true) // "true"
::dropbox::oauth/bool-param(null) // null
build-authorize-params
fn (opts: Map): Map
The Dropbox-specific authorization parameters for authorize-url
(pure). Options (all optional):
token-access-type-"offline"(default; returns a refresh token),"online"(access token only), or null to omitscope- Vec or space/comma separated Str of scopes (files.content.read,sharing.write, ...); omitted means every scope the app has in the App Consoleinclude-granted-scopes-"user"or"team"(incremental authorization)code-verifier- a PKCE verifier; its S256 challenge is sentcode-challenge- a precomputed S256 challenge (instead ofcode-verifier)force-reapprove,force-reauthentication,disable-signup- Boolsrequire-role-"personal"or"work"locale- e.g."de"extra- any other parameters, merged last
Example
built ::dropbox::oauth/build-authorize-params({scope: ["files.content.read"], locale: "de"})
built.params // {token_access_type: "offline", scope: "files.content.read", locale: "de"}
exchange-code
fn (app-key: Str, app-secret: Str?, code: Str, redirect-uri: Str?): Map
fn (app-key: Str, app-secret: Str?, code: Str, redirect-uri: Str?, opts: Map): Map
Exchange an authorization code for tokens at
https://api.dropboxapi.com/oauth2/token. Returns Dropbox's token
response (access_token, expires_in, token_type, refresh_token
when offline, scope, account_id, uid, team_id), or a
::dropbox::auth/TokenError. The tokens are marked as secrets.
app-secret may be null for a PKCE flow; redirect-uri must match the one sent to authorize-url
(null when none was). opts.code-verifier completes a PKCE flow.
Example
redirect "https://app.example.com/oauth/dropbox"
tokens ::dropbox::oauth/exchange-code("abc123appkey", ::hot::ctx/get("dropbox.app.secret"), "code-from-query", redirect)
tokens.refresh_token
// PKCE: no app secret, send the verifier from the authorize step
pkce ::dropbox::oauth/exchange-code("abc123appkey", null, "code-from-query", redirect, {
code-verifier: ::hot::ctx/get("oauth.pkce.verifier")
})
pkce.account_id
granted-scopes
fn (tokens: Map): Vec
The scopes a token response says were granted (scope split on spaces).
Example
::dropbox::oauth/granted-scopes({scope: "files.content.read sharing.write"}) // ["files.content.read", "sharing.write"]
missing-scopes
fn (tokens: Map, required: Str | Vec): Vec
The required scopes (Vec or space/comma separated Str) that a token
response did not grant. Empty when everything was granted.
Example
::dropbox::oauth/missing-scopes({scope: "files.content.read"}, ["files.content.read", "sharing.write"]) // ["sharing.write"]
refresh-access-token
fn (app-key: Str, app-secret: Str?, refresh-token: Str): Map
Exchange a refresh token for a fresh access token (no cache). Returns
the token response or a TokenError; a revoked refresh token yields
error-code: "invalid_grant", so ask the user to connect again.
Example
tokens ::dropbox::oauth/refresh-access-token("abc123appkey", ::hot::ctx/get("dropbox.app.secret"), ::hot::ctx/get("dropbox.refresh.token"))
tokens.access_token
revoke-token
fn (access-token: Str): Bool
POST /2/auth/token/revoke - revoke an access token. Dropbox also
disables the refresh token it came from and every other access token
minted from it, so this is how an app disconnects a user. Pass a
fresh access token (mint one with refresh-access-token when you
only stored the refresh token). Returns true, or a TokenError.
Example
// Disconnect this user: revokes the refresh token too
::dropbox::oauth/revoke-token(::dropbox::auth/access-token())
scope-str
fn (scope: Any): Str?
A scope value as Dropbox wants it (space-separated): a Vec is joined, a Str is passed through with commas turned into spaces, null stays null.
Example
::dropbox::oauth/scope-str(["files.content.read", "sharing.write"]) // "files.content.read sharing.write"
::dropbox::oauth/scope-str("files.content.read,sharing.write") // "files.content.read sharing.write"