xtekky/gpt4free · error · Exception
No valid access token obtained.
Error message
No valid access token obtained.
What it means
Raised by QwenContentGenerator.get_valid_token (qwenContentGenerator.py:36) when the shared token manager returned a credentials dict whose 'access_token' key is missing or empty. It means the Qwen OAuth credential store exists but is incomplete — typically because the device-code login never finished or the stored credentials were reset after a failed refresh.
Source
Thrown at g4f/Provider/qwen/qwenContentGenerator.py:36
def get_current_endpoint(self, resource_url: Optional[str]) -> str:
url = resource_url if resource_url else self.base_url
if not url.startswith("http"):
url = "https://" + url
if not url.endswith("/v1"):
url = url.rstrip("/") + "/v1"
return url
async def get_valid_token(self) -> Dict[str, str]:
"""
Obtain a valid token and endpoint from shared token manager.
"""
credentials = await self.shared_manager.getValidCredentials(self.qwen_client)
token = credentials.get("access_token")
resource_url = credentials.get("resource_url")
endpoint = self.get_current_endpoint(resource_url)
if not token:
raise Exception("No valid access token obtained.")
return {"token": token, "endpoint": endpoint}
View on GitHub (pinned to 973504e177)
Solutions
- Complete the Qwen OAuth device-code login so valid credentials are written to the token store
- Delete the stale credentials file and re-authenticate from scratch
- Check for TokenManagerError logs just before this — the root cause is usually a failed refresh (REFRESH_FAILED / expired refresh token)
- Ensure no other process holds the lock file while credentials are being refreshed
Defensive patterns
Strategy: validation
Validate before calling
async def has_valid_qwen_access(manager, client) -> bool:
creds = await manager.getValidCredentials(client)
return bool(creds.get("access_token")) Type guard
def has_access_token(creds: dict) -> bool:
return isinstance(creds, dict) and isinstance(creds.get("access_token"), str) and len(creds["access_token"]) > 0 Try / catch
try:
auth = await generator.get_valid_token()
except Exception as exc:
if "No valid access token" in str(exc):
await run_qwen_device_login() # re-authenticate
else:
raise Prevention
- Complete device authorization before enabling the Qwen provider
- Monitor refresh failures — they usually precede a missing access token
- Persist the credentials directory across container restarts
When it happens
Trigger: Calling any Qwen generation path that first awaits get_valid_token(); shared_manager.getValidCredentials(...) returns a dict without a truthy 'access_token' — e.g. empty credentials loaded from disk, or a refresh cycle that returned no token.
Common situations: First run before completing Qwen device authorization; credentials file partially written or hand-edited; refresh token expired and was wiped (see qwenOAuth2.py:177) leaving no access token; multiple processes racing on the credentials file.
Related errors
- GitHub Copilot OAuth not configured. Please run 'g4f auth gi
- NO_TOKEN
- Failed to read OAuth credentials from {path}: {e}
- No refresh token found in credentials.
- Token refresh failed: {text}
AI-assisted analysis of xtekky/gpt4free@973504e177 (2026-08-14).
Data as JSON: /api/errors/9820c6fd36098f6f.
Report an issue: GitHub.