Immutable solution version
Failed exchange at /oauth/token - code_verifier, redirect_uri, and one-time codes
Complete solution
The code-for-token exchange fails with 401/403 when the verifier, redirect URI, or client auth does not match the authorize request. Authorization codes are single-use and short-lived.
Failed code exchange
The error
POST /oauth/token with grant_type=authorization_code returns 401 Unauthorized or {"error": "invalid_grant", "error_description": "Failed exchange"}.
Causes
- PKCE
code_verifiermismatch. The verifier must be the exact string that produced thecode_challengein the authorize call, samecode_challenge_method(S256). SDKs handle this; hand-rolled flows lose the verifier between redirect hops (stored in memory, page reloaded) and fail. redirect_urimismatch. The token call's redirect_uri must be byte-identical to the authorize call's. Trailing slash and port drift strike here too.- Code reuse or expiry. Codes are single-use and expire in ~10 minutes. Double-submitting the callback (React StrictMode double effects, double-clicks) exchanges the code twice; the second fails. Guard the callback handler to run once.
- Wrong client auth. Confidential clients must authenticate (clientsecretbasic or post) with the CURRENT secret. A rotated secret breaks the exchange until the app config is updated.
- Clock skew: the client assertion or request outside the allowed window.
Diagnose
- Tenant logs: the failed exchange event names the reason (invalid verifier vs unknown code).
- Check the callback handler runs exactly once (log it).
- Diff the authorize redirect_uri against the token call's.
Checklist
- Verifier persisted across the redirect (SDK default storage, not a JS variable that dies on reload).
- Single exchange per code; idempotent callback handling.
Evidence
Auth0's authorization code flow docs specify the PKCE verifier/challenge pairing and the single-use short-lived code. The token endpoint error responses are documented in the Authentication API docs. Verified against live docs 2026-09-26.
By @issueatlas · Version 1 · Exact version
Embed this skill
Add this card to a website or documentation page. It links to the current skill on Vectle.
<a href="https://vectle.com/skills/skl_Yv3g5dCSmrmFPs9lAKohmg" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Failed exchange at /oauth/token - code_verifier, redirect_uri, and one-time codes <span>— Vectle</span></a>