Prompt
How do I use OAuth for API access on behalf of users?
Latest observation
Nov 13, 2026
OpenAI APIWeb search: off
To use OAuth for API access on behalf of users, you typically use OAuth 2.0 Authorization Code flow (often with PKCE). This lets your app get a user’s permission, then receive an access token that your app can use to call the API as that user.
High-level flow
-
Register your app with the API provider
- Get a
client_id - Set a
redirect_uri - Configure requested scopes
- Get a
-
Send the user to the authorization URL
- User logs in to the provider
- User approves requested access
-
Provider redirects back to your app
- Usually with an authorization
code
- Usually with an authorization
-
Exchange the code for tokens
- Send the code to the token endpoint
- Receive:
access_token- optionally
refresh_token - optionally
id_tokenif using OpenID Connect
-
Call the API
- Include the access token in the
Authorization: Bearer ...header
- Include the access token in the
-
Refresh tokens when needed
- Use the refresh token to get a new access token when the old one expires
Example authorization request
Redirect the user to something like:
https://provider.example.com/oauth/authorize?
response_type=code&
client_id=YOUR_CLIENT_ID&
redirect_uri=https%3A%2F%2Fyourapp.com%2Fcallback&
scope=read%20write&
state=RANDOM_CSRF_TOKEN&
code_challenge=...&
code_challenge_method=S256
Important parameters
response_type=code: use authorization code flowclient_id: your app’s client IDredirect_uri: where the provider sends the user backscope: permissions requestedstate: random value to prevent CSRFcode_challenge: PKCE protectioncode_challenge_method=S256: recommended PKCE method
Token exchange
After redirect, your app receives:
https://yourapp.com/callback?code=AUTH_CODE&state=RANDOM_CSRF_TOKEN
Then your backend exchanges the code for tokens:
POST /oauth/token
Content-Type: application/x-www-form-urlencoded
grant_type=authorization_code&
code=AUTH_CODE&
redirect_uri=https://yourapp.com/callback&
client_id=YOUR_CLIENT_ID&
code_verifier=YOUR_ORIGINAL_CODE_VERIFIER
The response often looks like:
{
"access_token": "eyJ...",
"token_type": "Bearer",
"expires_in": 3600,
"refresh_token": "def...",
"scope": "read write"
}
Calling the API
Use the access token:
GET /api/user/profile
Authorization: Bearer eyJ...
Refreshing an expired access token
POST /oauth/token
Content-Type: application/x-www-form-urlencoded
grant_type=refresh_token&
refresh_token=REFRESH_TOKEN&
client_id=YOUR_CLIENT_ID
Best practices
- Use Authorization Code + PKCE for browser/mobile apps
- Never expose client secrets in frontend code
- Validate the
stateparameter - Store tokens securely
- Request only the scopes you need
- Handle token expiration and revocation
- Use HTTPS only
Which flow should you use?
- Web app with backend: Authorization Code flow
- SPA (single-page app): Authorization Code + PKCE
- Mobile app: Authorization Code + PKCE
- Machine-to-machine (no user): Client Credentials flow, not user-based OAuth
If you want, I can also give you:
- a step-by-step diagram
- sample code in Python/Node/JavaScript
- or the flow for a specific provider like Google, Microsoft, GitHub, or Slack