xtekky/gpt4free · error · TokenManagerError
TOKEN_ERROR
TOKEN_ERROR
Error message
Failed to get Copilot token: {resp.status} - {text} What it means
TokenManagerError with code TOKEN_ERROR, raised when the Copilot token exchange returns any non-200/non-401 status. The status code and response body are embedded in the message, covering server-side and unexpected-client errors (403 rate limit/entitlement, 404 bad headers, 5xx outages) during the OAuth-to-Copilot-token exchange.
Source
Thrown at g4f/Provider/github/copilotTokenProvider.py:76
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
from datetime import datetime
dt = datetime.fromisoformat(expires_at.replace("Z", "+00:00"))
self._copilot_token_expires_at = dt.timestamp()
except Exception:
# Default to 30 minutes from now if parsing failsView on GitHub (pinned to 973504e177)
Solutions
- Inspect the embedded status and body text: 403 usually means the account has no Copilot access — verify the subscription
- Back off and retry for rate-limit/5xx responses; the exchange endpoint is rate-limited
- Update g4f to the latest version so editor headers stay current
Defensive patterns
Strategy: retry
Try / catch
try:
token = await provider.get_valid_token()
except TokenManagerError as e:
if e.code == 'TOKEN_ERROR' and (' 429 ' in str(e) or ' 5' in str(e)[:40]):
await asyncio.sleep(backoff())
token = await provider.get_valid_token()
raise Prevention
- Cache the Copilot token for its full lifetime (the provider already caches until expiry-60s) instead of re-exchanging per request
- Add jittered backoff on exchange failures
- Keep g4f updated so editor headers match GitHub's expectations
When it happens
Trigger: GET copilot_internal/token returning 403 (account lacks Copilot entitlement or rate-limited), 404 (editor-version headers rejected), or 5xx (GitHub incident) while exchanging a structurally valid GitHub token.
Common situations: Copilot subscription not active on the GitHub account; GitHub secondary rate limits during rapid token refreshes; library's EDITOR_VERSION headers outdated after GitHub API changes.
Related errors
- Failed to chat: {response.status} {error_text}
- Failed to fetch Copilot usage: {resp.status} {text}
- AUTH_FAILED
- Cannot create GLM chat session (status {response.status})
- Failed to decode JSON from PhindAi response: {text}
AI-assisted analysis of xtekky/gpt4free@973504e177 (2026-08-14).
Data as JSON: /api/errors/83fcab4811becaad.
Report an issue: GitHub.