python auth_error ai_generated true

fastapi.exceptions.HTTPException: 401 Unauthorized: {"detail":"Not authenticated"}

ID: python/fastapi-oauth2-missing-token

Also available as: JSON · Markdown · 中文
80%Fix Rate
88%Confidence
0Evidence
2025-01-09First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
0.100.x active — — —
0.110.x active — — —

Root Cause

OAuth2PasswordBearer dependency did not receive an Authorization: Bearer header, or the token was rejected.

generic

中文

OAuth2PasswordBearer依赖未收到Authorization: Bearer头,或token被拒绝。

Workarounds

  1. 95% success
    headers = {'Authorization': f'Bearer {token}'}\nrequests.get(url, headers=headers)
  2. 90% success
    from fastapi.security import HTTPBearer\nsecurity = HTTPBearer(auto_error=False)

Dead Ends

Common approaches that don't work:

  1. 80% fail

    Returns None token and downstream logic still fails.

  2. 85% fail

    OAuth2PasswordBearer expects the Authorization header by default.