python auth_error ai_generated true

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

ID: python/fastapi-oauth2-token-missing

Also available as: JSON · Markdown · 中文
80%Fix Rate
88%Confidence
0Evidence
2024-06-28First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
0.100+ active — — —

Root Cause

The Authorization header was missing or malformed, so the OAuth2PasswordBearer dependency rejected the request before reaching the route.

generic

中文

Authorization 头缺失或格式错误,导致 OAuth2PasswordBearer 依赖在到达路由前拒绝了请求。

Workarounds

  1. 95% success
    fetch('/me', {headers: {'Authorization': `Bearer ${token}`}})
  2. 85% success
    def get_token(request: Request):
        return request.cookies.get('access_token')

Dead Ends

Common approaches that don't work:

  1. 80% fail

    Route then receives None token and crashes on decode.

  2. 90% fail

    Auth bypass ships to production; security hole.