xtekky/gpt4free · warning · RuntimeError

Could not extract authorization code

Error message

Could not extract authorization code

What it means

Manual login flow: the user pasted something, but no authorization code could be extracted from it. If the input started with 'http', urlparse found no 'code' query parameter; if it was plain text, the code variable is the raw input — falsy only if it strips to nothing, so in practice this fires for URLs whose query string lacks code (consent cancelled) or whose parameters were truncated.

Source

Thrown at g4f/Provider/needs_auth/Antigravity.py:907

            user_input = input("Paste redirect URL or code: ").strip()

            if not user_input:
                raise RuntimeError("No input provided")

            # Parse the input
            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:
                # Assume it's just the code
                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_and_save(
        cls,
        project_id: str = "",
        no_browser: bool = False,
        credentials_path: Optional[Path] = None,
    ) -> "AntigravityAuthManager":
        """

View on GitHub (pinned to 973504e177)

Solutions

  1. Re-run login, approve consent, then paste the FULL redirect URL shown in the address bar after being redirected to localhost.
  2. Verify the pasted URL contains a 'code=' query parameter before submitting.
  3. If the URL shows error=access_denied, consent was denied — see error 108 remedies.
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"):
        params = parse_qs(urlparse(user_input).query)
        return params.get("code", [None])[0]
    return user_input or None

# before pasting, verify:
assert extract_code(pasted), "redirect URL has no code= parameter"

Type guard

def url_contains_code(url: str) -> bool:
    """True when the redirect URL's query string carries a code parameter."""
    try:
        return "code" in parse_qs(urlparse(url).query)
    except Exception:
        return False

Try / catch

try:
    tokens = await Antigravity.login()
except RuntimeError as e:
    if "Could not extract authorization code" in str(e):
        # user pasted wrong/truncated URL; restart and copy the full localhost URL
        raise

Prevention

When it happens

Trigger: Pasted URL like http://localhost:PORT/callback?error=access_denied&state=... (no code param), a URL with the query string cut off, or a URL where '#fragment' encoding hid the parameters.

Common situations: User denied consent (redirect carries error= instead of code=); copied the URL from a page that truncated it; pasted the authorization-page URL instead of the post-consent redirect URL.

Related errors


AI-assisted analysis of xtekky/gpt4free@973504e177 (2026-08-14). Data as JSON: /api/errors/043584fff664b1ef. Report an issue: GitHub.