xtekky/gpt4free · error · RuntimeError

No authorization code received

Error message

No authorization code received

What it means

The callback server received a redirect request, but the query parameters contained no 'code'. In Google's OAuth redirect this happens when consent fails — the redirect then carries error=access_denied (or an admin policy blocks it) instead of an authorization code.

Solutions

  1. Restart login and click Allow on the consent screen
  2. If access is blocked by an admin policy, use a personal or allowed account
  3. Check the callback URL's other params (error, error_description) to confirm denial
  4. Make sure only the login flow uses the callback port
Defensive patterns

Strategy: try-catch

Validate before calling

from urllib.parse import urlparse, parse_qs

def callback_has_code(redirect_url: str) -> bool:
    q = parse_qs(urlparse(redirect_url).query)
    return bool(q.get("code", [None])[0]) and "error" not in q

Try / catch

try:
    tokens = await GeminiCLI.login()
except RuntimeError as e:
    if "No authorization code received" in str(e):
        # consent denied or blocked: instruct user, do not auto-retry
        raise AuthUserAction("approve the Google consent screen and retry")

Prevention

When it happens

Trigger: User clicks Cancel or Deny on the Google consent screen; Workspace admin policy auto-denies the app; the callback URL is hit by something else (health check, favicon request) with no code parameter.

Common situations: Accidental cancellation; the OAuth client flagged as unverified so users back out; automated environments probing the localhost port.

Related errors


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

Appendix: source

Thrown at g4f/Provider/needs_auth/GeminiCLI.py:1223

                print("You may need to close any application using that port.\n")

            print(f"\nPlease open this URL in your browser:\n")
            print(f"{auth_url}\n")

        if server_started:
            print("Waiting for authentication callback...")

            try:
                callback_result = callback_server.wait_for_callback()

                if not callback_result:
                    raise RuntimeError("OAuth callback timed out")

                code = callback_result.get("code")
                callback_state = callback_result.get("state")

                if not code:
                    raise RuntimeError("No authorization code received")

                print("\n✓ Authorization code received. Exchanging for tokens...")

                tokens = await cls.exchange_code_for_tokens(
                    code, callback_state or state
                )

                print(f"✓ Authentication successful!")
                if tokens.get("email"):
                    print(f"  Logged in as: {tokens['email']}")

                return tokens

            finally:
                callback_server.stop()
        else:
            print(
                "\nAfter completing authentication, you'll be redirected to a localhost URL."

View on GitHub (pinned to 973504e177)