OAuth2 Invalid Grant Error Fix
In this tutorial, you'll learn about OAuth2 Invalid Grant Error Fix. We cover key concepts, practical examples, and best practices.
You exchange an authorization code for a token and get error: invalid_grant — the authorization code expired, was already used, or the grant type in the request does not match the authorization flow.
Step-by-Step Fix
1. Check if the authorization code expired
Authorization codes are short-lived (usually 1-10 minutes):
# Wrong — using a code after it expired
import requests
# Code was obtained 15 minutes ago
response = requests.post("https://oauth.example.com/token", data={
"grant_type": "authorization_code",
"code": "expired_code_123",
"redirect_uri": "https://myapp.com/callback",
"client_id": "my-client-id",
"client_secret": "my-secret",
})
# {"error": "invalid_grant"}
2. Re-authorize to get a fresh code
# Right — get a fresh authorization code
from requests_oauthlib import OAuth2Session
client = OAuth2Session("my-client-id", redirect_uri="https://myapp.com/callback")
authorization_url, state = client.authorization_url("https://oauth.example.com/auth")
# User visits URL, authorizes, gets redirected with ?code=...
# Exchange immediately — don't wait
token = client.fetch_token(
"https://oauth.example.com/token",
code=response_code,
client_secret="my-secret",
)
3. Fix grant type mismatch
# Wrong — using client_credentials grant for an auth_code flow
response = requests.post("https://oauth.example.com/token", data={
"grant_type": "client_credentials",
"client_id": "my-client-id",
"client_secret": "my-secret",
})
# Right — use the correct grant type
response = requests.post("https://oauth.example.com/token", data={
"grant_type": "authorization_code",
"code": "fresh_code_456",
"redirect_uri": "https://myapp.com/callback",
"client_id": "my-client-id",
"client_secret": "my-secret",
})
4. Fix refresh token flow
# Wrong — using expired refresh token
response = requests.post("https://oauth.example.com/token", data={
"grant_type": "refresh_token",
"refresh_token": "expired_refresh_token",
"client_id": "my-client-id",
"client_secret": "my-secret",
})
# Right — use a valid refresh token or re-authenticate
if token_expired:
try:
new_token = client.refresh_token(
"https://oauth.example.com/token",
refresh_token=stored_refresh_token,
)
except:
# Refresh token invalid — re-authenticate
authorization_url, state = client.authorization_url("https://oauth.example.com/auth")
Common Mistakes
| Mistake | Fix |
|---|---|
| Using the same authorization code twice | Codes are single-use. Get a new code for each token request. |
| Waiting too long to exchange the code | Exchange the code immediately after receiving it. |
| Wrong grant_type parameter | Use authorization_code for auth code flow, refresh_token for refresh. |
| Missing or wrong redirect_uri | The redirect_uri must match the one used in the authorization request. |
| Using PKCE code_verifier without code_challenge | If PKCE is used, send both code_verifier and the original code_challenge_method. |
Prevention
- Exchange authorization codes immediately after receiving them.
- Store refresh tokens securely and replace them on refresh.
- Use PKCE for public clients (SPAs, mobile apps).
- Log all token endpoint responses for debugging.
DodaTech Tools
Doda Browser's token inspector decodes JWT tokens and validates OAuth2 grant flows for debugging. DodaZIP encrypts and archives token caches for security auditing. Durga Antivirus Pro monitors for authorization code interception attacks.
Common Mistakes with invalid grant
- Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists - Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
These mistakes appear frequently in real-world OAUTH2 code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro