xtekky/gpt4free · warning · RuntimeError
Could not extract authorization code
Error message
Could not extract authorization code
What it means
The manually pasted input was treated as a redirect URL, but urlparse/parse_qs found no 'code' query parameter — the URL is the wrong page (for example the consent page itself, or a post-consent landing page without ?code=), or the query string was truncated.
Source
Thrown at g4f/Provider/needs_auth/GeminiCLI.py:1262
"Copy and paste the full redirect URL or just the authorization code below:\n"
)
user_input = input("Paste redirect URL or code: ").strip()
if not user_input:
raise RuntimeError("No input provided")
if user_input.startswith("http"):
parsed = urlparse(user_input)
params = parse_qs(parsed.query)
code = params.get("code", [None])[0]
callback_state = params.get("state", [state])[0]
else:
code = user_input
callback_state = state
if not code:
raise RuntimeError("Could not extract authorization code")
print("\nExchanging code for tokens...")
tokens = await cls.exchange_code_for_tokens(code, callback_state)
print(f"✓ Authentication successful!")
if tokens.get("email"):
print(f" Logged in as: {tokens['email']}")
return tokens
@classmethod
async def login(
cls,
no_browser: bool = False,
credentials_path: Optional[Path] = None,
) -> "AuthManager":
"""
Perform interactive OAuth login and save credentials.View on GitHub (pinned to 973504e177)
Solutions
- Copy the complete localhost redirect URL, including the full ?code=...&state=... query string
- If the browser blocks the localhost redirect, copy the code from the page Google shows ('copy this code') and paste the raw code instead
- Avoid line-wrapping when pasting; paste as one line
Defensive patterns
Strategy: validation
Validate before calling
from urllib.parse import urlparse, parse_qs
def extract_code(user_input: str):
if user_input.startswith("http"):
return parse_qs(urlparse(user_input).query).get("code", [None])[0]
return user_input or None
if extract_code(user_input) is None:
print("that URL has no ?code= param; copy the localhost redirect URL or the raw code") Try / catch
try:
tokens = await GeminiCLI.login()
except RuntimeError as e:
if "Could not extract authorization code" in str(e):
# re-prompt with clearer instructions (use Google's 'copy code' fallback)
pass Prevention
- Paste the full final localhost redirect URL on one line
- If the browser hides the redirect, use Google's 'copy this code' button and paste the raw code
When it happens
Trigger: User pastes a URL that is not the final localhost redirect: no ?code= param, or the query portion was cut off during copy; parse_qs returns code=None.
Common situations: Copying the address of the Google consent page instead of the resulting redirect; browser shows about:blank or blocks localhost navigation so the real redirect URL is never visible; terminal mangles long URLs.
Related errors
- No input provided
- Missing PKCE verifier in state parameter
- Token exchange failed: {error_text}
- Missing tokens in response
- OAuth callback timed out
AI-assisted analysis of xtekky/gpt4free@973504e177 (2026-08-14).
Data as JSON: /api/errors/7dbf9949ff968537.
Report an issue: GitHub.