POST /oauth/token • HTTP 200 OK

OAuth 2.0 Authorization Code Token Exchange — Request & Response Example

The backbone of "Sign in with Google/GitHub": swapping a temporary authorization code for long-term API access credentials.

1. HTTP Client Request (POST)

POST /oauth/token HTTP/1.1
{
  "Content-Type": "application/x-www-form-urlencoded"
}

{
  "grant_type": "authorization_code",
  "code": "splat_code_8849201948",
  "redirect_uri": "https://myapp.com/callback",
  "client_id": "client_77812903",
  "client_secret": "sec_0918237461928"
}

2. Server JSON Response (HTTP 200 OK)

{
  "access_token": "ya29.a0ARrdaM982...",
  "token_type": "Bearer",
  "expires_in": 3600,
  "refresh_token": "1//0gX89a...",
  "scope": "user:email read:profile"
}

Copy-Paste Code Snippets (cURL, JavaScript Fetch, Python, Node.js)

curl -X POST "https://cometflow.space/oauth/token" \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "grant_type=authorization_code&code=splat_code_8849201948&client_id=client_77812903"
fetch('https://cometflow.space/oauth/token', {
  method: 'POST',
  headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
  body: new URLSearchParams({
    grant_type: 'authorization_code',
    code: 'splat_code_8849201948',
    client_id: 'client_77812903'
  })
}).then(res => res.json()).then(console.log);
import requests

payload = {
    "grant_type": "authorization_code",
    "code": "splat_code_8849201948",
    "client_id": "client_77812903"
}
res = requests.post("https://cometflow.space/oauth/token", data=payload)
print(res.json())

How This API Call Works Step-by-Step

  1. 1. User approves permissions on consent screen.
  2. 2. Provider redirects with ?code=123.
  3. 3. Backend server POSTs code + client_secret to /oauth/token.
  4. 4. Provider validates credentials and issues access_token (1 hr lifespan) and refresh_token.

Backend API Functions: The API function exchangeOAuthCode() checks that the code has not expired, verifies client_secret, signs a new access token, and invalidates the code permanently.

Frequently Asked Questions

Why does OAuth use both an authorization code and an access token?

The authorization code passes through the user’s browser URL, which is less secure. The actual access token is exchanged back-channel directly from server-to-server, preventing token leakage in browser history.