python
auth_error
ai_generated
true
HTTPException 401: {"detail":"Not authenticated"}
ID: python/fastapi-oauth2-token-401
80%Fix Rate
86%Confidence
0Evidence
2024-06-19First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 0.100+ | active | — | — | — |
| 0.110+ | active | — | — | — |
Root Cause
OAuth2PasswordBearer(tokenUrl=...) receives no Authorization header, or tokenUrl path doesn't match the actual login route.
generic中文
OAuth2PasswordBearer(tokenUrl=...) 未收到 Authorization 头,或 tokenUrl 路径与实际登录路由不匹配。
Workarounds
-
92% success
oauth2_scheme = OAuth2PasswordBearer(tokenUrl='/auth/token') # must match @app.post('/auth/token') -
93% success
Client: headers={'Authorization': f'Bearer {token}'}
Dead Ends
Common approaches that don't work:
-
70% fail
Token becomes None; downstream still fails auth.
-
95% fail
Security regression; endpoints become public.