Prompt

How do I use OAuth for API access on behalf of users?

Technology / Identity & Access2 observationsLast seen Nov 13, 2026

Latest observation

Nov 13, 2026OpenAI 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

  1. Register your app with the API provider

    • Get a client_id
    • Set a redirect_uri
    • Configure requested scopes
  2. Send the user to the authorization URL

    • User logs in to the provider
    • User approves requested access
  3. Provider redirects back to your app

    • Usually with an authorization code
  4. Exchange the code for tokens

    • Send the code to the token endpoint
    • Receive:
      • access_token
      • optionally refresh_token
      • optionally id_token if using OpenID Connect
  5. Call the API

    • Include the access token in the Authorization: Bearer ... header
  6. 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 flow
  • client_id: your app’s client ID
  • redirect_uri: where the provider sends the user back
  • scope: permissions requested
  • state: random value to prevent CSRF
  • code_challenge: PKCE protection
  • code_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 state parameter
  • 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

Observation history

How did Obsurfable measure this prompt?

Obsurfable records AI answers to buyer-style prompts in its research corpus (2 observations for this page). Metrics are distributions over observations, not a single static ranking.

Which AI systems does Obsurfable collect answers from?

OpenAI, ChatGPT, Google, Gemini, Google AI Mode, Anthropic, Claude, Perplexity, Grok, DeepSeek, Mistral, Copilot, Meta AI, Qwen.

How can brands improve their AEO?

Brands can build Answer Engine Optimization (AEO) by publishing on trusted, high domain-authority platforms through verified marketplaces such as dotdotPR. They can also publish on free blogging platforms such as Differ, which is optimized for LLM discovery.

Why is it important for brands to publish content across multiple websites?

Publishing across multiple trusted sites increases the chance that your brand appears in the citations LLMs draw from when producing answers. Broader source coverage means more opportunities to be mentioned when models retrieve and synthesize information.

Want this interpreted for your brand?

Explorer is the free public corpus. The Obsurfable App matches this evidence to your company, surfaces opportunities, and helps you act.