Token exchange: request shape and reading error codes
In one line: Required parameters for the authorization_code grant, client authentication methods, and how to tell invalid_grant from invalid_client.
Receiving a code on the callback is not the end of the login. The server (or a BFF) must exchange it at the token endpoint for an access token, ID token and possibly a refresh token. Failures here are almost always parameter mismatches.
Request shape
POST /token HTTP/1.1 Host: idp.example.com Content-Type: application/x-www-form-urlencoded grant_type=authorization_code &code=SplxlOBeZQQYbYS6WxSbIA &redirect_uri=https%3A%2F%2Fapp.example.com%2Fcallback &client_id=oidc-demo-client &code_verifier=dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk
Confidential clients authenticate with Authorization: Basic base64(client_id:client_secret) or client_secret_post. Public clients send the PKCE verifier instead. Never embed a client_secret in browser code.
Response
{
"access_token": "2YotnFZFEjr1zCsicMWpAA",
"token_type": "Bearer",
"expires_in": 3600,
"refresh_token": "tGzv3JOkF0XG5Qx2TlKWIA",
"id_token": "eyJhbGciOiJSUzI1NiIs..."
}
Error codes
| Error | Meaning | Check |
|---|---|---|
invalid_request | Missing or duplicate parameter | grant_type, code, redirect_uri, code_verifier |
invalid_client | Client authentication failed | client_id/secret, Basic encoding, public vs confidential registration |
invalid_grant | Code, verifier or redirect_uri mismatch; expired or reused | PKCE pair, code lifetime (often 10 minutes), single use |
unauthorized_client | Flow not allowed for this client | Grant type settings in the provider console |
unsupported_grant_type | Grant type not supported | Typo, provider coverage |
invalid_scope | Scope refused | Configured consent items, scope names |
Providers often merge causes into invalid_grant for security. Cross-check server and provider audit logs.
Validating the ID token
An ID token is a JWT. Verify the signature against JWKS and check iss, aud, exp and nonce, and never accept alg: none. Access tokens may be JWTs or opaque strings; opaque tokens are checked with userinfo or introspection.
Refresh tokens
Many providers rotate refresh tokens. When a new one arrives, discard and replace the old value. Public clients should combine rotation with PKCE.
Checking it in OAuthLab
Panel ⑥ builds cURL, fetch and Python snippets from your current values. Put a real code in place, and load the client secret from a server environment variable rather than the snippet. This page sends no requests.
PKCE and URL checks run right here in your browser. Nothing you type is stored.
Open OAuthLabFrequently asked questions
Can the token exchange happen in the browser?
Public clients can do it with PKCE, but a confidential client that needs a client_secret or refresh token should exchange on the server (BFF).
What happens if I use a code twice?
The second request fails with invalid_grant. Some providers also revoke the first token, so avoid duplicate exchanges on network retries.
How do I know whether an access token is a JWT?
Three dot-separated segments means JWT. Never trust its claims without verifying the signature; opaque Tokens need introspection.
Sources
- RFC 6749 — The OAuth 2.0 Authorization Framework
- RFC 6750 — The OAuth 2.0 Bearer Token Usage
- RFC 7636 — Proof Key for Code Exchange by OAuth Public Clients
Read next
- PKCE S256: computing code_verifier and code_challenge correctlyThe RFC 7636 character set and length rules, the SHA-256 to Base64URL step, and where each value travels in the authorization and token requests.
- Authorization URL parameters: what to send and whyauthorization_endpoint, client_id, redirect_uri, response_type, scope, state, nonce, prompt and code_challenge, with a complete example URL.
- redirect_uri registration rules and open redirect riskWhy exact matching matters, and how wildcards, subdomains, path traversal and userinfo (@) confusion turn into account takeover.