python
auth_error
ai_generated
true
HTTPException 401: {"detail":"未认证"}
HTTPException 401: {"detail":"Not authenticated"}
ID: python/fastapi-oauth2-token-401
80%修复率
86%置信度
0证据数
2024-06-19首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 0.100+ | active | — | — | — |
| 0.110+ | active | — | — | — |
根因分析
OAuth2PasswordBearer(tokenUrl=...) 未收到 Authorization 头,或 tokenUrl 路径与实际登录路由不匹配。
English
OAuth2PasswordBearer(tokenUrl=...) receives no Authorization header, or tokenUrl path doesn't match the actual login route.
解决方案
-
92% 成功率
oauth2_scheme = OAuth2PasswordBearer(tokenUrl='/auth/token') # must match @app.post('/auth/token') -
93% 成功率
Client: headers={'Authorization': f'Bearer {token}'}
无效尝试
常见但无效的做法:
-
70% 失败
Token becomes None; downstream still fails auth.
-
95% 失败
Security regression; endpoints become public.