Describe the problem/error/question
Gmail OAuth2 authentication consistently fails with 401 Unauthorized during the callback phase, despite Google Cloud being configured correctly. Manual token exchange using curl succeeds, proving the issue is within n8n’s OAuth implementation.
**Related issue:** Gmail OAuth2 credential fails during authorization on a self-hosted n8n instance (same symptoms, but I have additional debugging evidence)
What is the error message (if any)?
When the OAuth callback returns to n8n:
Status: 401 Unauthorized Response: {“status”:“error”,“message”:“Unauthorized”}
Browser shows: “Error: Unauthorized - Failed to connect. The window can be closed now.”
n8n debug logs show OAuth URL generation but **no error logs** when the callback fails.
Please share your workflow
N/A - This occurs during credential setup, before any workflow is created. The issue happens when clicking "Sign in with Google" in the Gmail OAuth2 API credential configuration.
Share the output returned by the last node
N/A - No workflow execution. The failure occurs during OAuth credential authorization.
Information on your n8n setup
- n8n version: 2.4.6
- Database (default: SQLite): SQLite
- n8n EXECUTIONS_PROCESS setting (default: own, main): own, main
- Running n8n via (Docker, npm, n8n cloud, desktop app): Docker
- Operating system: macOS host running Docker (Ubuntu 24 container)
Docker Environment Variables:
- N8N_HOST=0.0.0.0
- N8N_PORT=5678
- NODE_ENV=production
- N8N_SECURE_COOKIE=false
- N8N_EDITOR_BASE_URL=http://localhost:5678
- WEBHOOK_URL=http://localhost:5678/
- GENERIC_TIMEZONE=Europe/Berlin
- TZ=Europe/Berlin
- N8N_LOG_LEVEL=debug
`
Google Cloud Configuration (verified working):
- Gmail API: Enabled

- OAuth Consent Screen: Testing mode
- Test user added: [email protected]

- OAuth Client Type: Web application
- Authorized redirect URI:
http://localhost:5678/rest/oauth2-credential/callback - Quotas: 0% usage, no limits reached
Steps to Reproduce
- Create new “Gmail OAuth2 API” credential in n8n Settings → Credentials
- Enter Client ID and Client Secret from Google Cloud Console
- Click “Sign in with Google”
- Browser redirects to Google - consent screen appears
- Click “Allow” to authorize all requested Gmail scopes
- Google redirects back to:
http://localhost:5678/rest/oauth2-credential/callback?state=...&code=...&scope=... - n8n returns 401 Unauthorized
What I’ve Verified
Manual Token Exchange Works
I captured the authorization code from the callback URL and manually exchanged it:
curl -X POST https://oauth2.googleapis.com/token \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "code=4/0ASc3gC0..." \
-d "client_id=MY_CLIENT_ID" \
-d "client_secret=MY_CLIENT_SECRET" \
-d "redirect_uri=http://localhost:5678/rest/oauth2-credential/callback" \
-d "grant_type=authorization_code"
Result:
HTTP 200 Success - Google returned valid access_token and refresh_token
This proves:
- Google Cloud configuration is correct
- Client credentials are valid
- Authorization code is valid
- The problem is within n8n’s OAuth callback handler
n8n Callback Consistently Fails
Browser network inspection shows n8n receives the same valid parameters from Google, but returns 401.
What I’ve Tried
Updated n8n: 2.1.4 → 2.4.6
Created multiple fresh OAuth credentials in Google Cloud
Generated new Client Secrets
Deleted and recreated credentials in n8n multiple times
Cleared browser cache (Safari and Chrome)
Verified Docker container time matches host time
Full Docker restart
Tested both Testing and Production OAuth consent modes
Created completely new Google Cloud project
Verified redirect URI matches exactly (no trailing slashes, correct port)
Nothing resolves the issue. The 401 error is consistent across all attempts.
Additional Context
- Other OAuth credentials work (e.g., Google Gemini API with API keys)
- Issue is specific to Gmail OAuth2
- n8n debug logs show OAuth URL generation but no errors during callback
- Same Google credentials work perfectly with manual curl
- Multiple Google accounts tested - same result
Question
Is this a known issue with n8n’s Gmail OAuth2 implementation in version 2.4.6? The fact that manual token exchange succeeds but n8n’s callback fails suggests an issue in n8n’s OAuth handler code.
Are there any additional environment variables, debugging steps, or workarounds available?
Thank you!