opyebOAuthLab

Token exchange: request shape and reading error codes

Tokens · 8 min read

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

ErrorMeaningCheck
invalid_requestMissing or duplicate parametergrant_type, code, redirect_uri, code_verifier
invalid_clientClient authentication failedclient_id/secret, Basic encoding, public vs confidential registration
invalid_grantCode, verifier or redirect_uri mismatch; expired or reusedPKCE pair, code lifetime (often 10 minutes), single use
unauthorized_clientFlow not allowed for this clientGrant type settings in the provider console
unsupported_grant_typeGrant type not supportedTypo, provider coverage
invalid_scopeScope refusedConfigured 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 OAuthLab

Frequently 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

Read next