xtekky/gpt4free · error · TokenManagerError

AUTH_FAILED

AUTH_FAILED

Error message

GitHub token is invalid or expired. Please login again.

What it means

TokenManagerError with code AUTH_FAILED, raised when the Copilot token exchange endpoint (https://api.github.com/copilot_internal/token) answers HTTP 401. The stored GitHub OAuth token was rejected as invalid or expired, so exchanging it for a Copilot API token is impossible and a fresh login is required.

Source

Thrown at g4f/Provider/github/copilotTokenProvider.py:70

        github_token = github_creds["access_token"]

        # Exchange for Copilot token
        async with aiohttp.ClientSession() as session:
            async with session.get(
                self.COPILOT_TOKEN_URL,
                headers={
                    "Authorization": f"token {github_token}",
                    "Accept": "application/json",
                    "User-Agent": USER_AGENT,
                    "Editor-Version": EDITOR_VERSION,
                    "Editor-Plugin-Version": EDITOR_PLUGIN_VERSION,
                    "Openai-Organization": "github-copilot",
                    "X-GitHub-Api-Version": API_VERSION,
                },
            ) as resp:
                if resp.status == 401:
                    raise TokenManagerError(
                        "AUTH_FAILED",
                        "GitHub token is invalid or expired. Please login again.",
                    )
                if resp.status != 200:
                    text = await resp.text()
                    raise TokenManagerError(
                        "TOKEN_ERROR",
                        f"Failed to get Copilot token: {resp.status} - {text}",
                    )

                data = await resp.json()
                self._copilot_token = data.get("token")

                # Parse expiration
                expires_at = data.get("expires_at")
                if expires_at:
                    try:
                        # Parse ISO format datetime

View on GitHub (pinned to 973504e177)

Solutions

  1. Re-run 'g4f auth github-copilot' to obtain a fresh OAuth token
  2. Check https://github.com/settings/applications and remove stale g4f grants, then re-authenticate
  3. Delete the credential file first if re-login keeps failing, to force a clean device flow
Defensive patterns

Strategy: fallback

Try / catch

try:
    token = await provider.get_valid_token()
except TokenManagerError as e:
    if e.code == 'AUTH_FAILED':
        await run_device_login()  # non-retryable: must re-login
        token = await provider.get_valid_token()

Prevention

When it happens

Trigger: GET copilot_internal/token with Authorization: token <github_token> returning 401 — revoked OAuth app grant, expired token, user password reset/2FA change invalidating tokens, or a truncated/corrupted stored token.

Common situations: The GitHub OAuth grant was revoked in GitHub settings; long-lived credentials that GitHub invalidated; manual editing of the credentials file corrupting the token string.

Understand the failure class

Related errors


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