python
auth_error
ai_generated
true
fastapi.exceptions.HTTPException: 401 未授权:未认证
fastapi.exceptions.HTTPException: 401 Unauthorized: {"detail":"Not authenticated"}
ID: python/fastapi-oauth2-missing-token
80%修复率
88%置信度
0证据数
2025-01-09首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 0.100.x | active | — | — | — |
| 0.110.x | active | — | — | — |
根因分析
OAuth2PasswordBearer依赖未收到Authorization: Bearer头,或token被拒绝。
English
OAuth2PasswordBearer dependency did not receive an Authorization: Bearer header, or the token was rejected.
解决方案
-
95% 成功率
headers = {'Authorization': f'Bearer {token}'}\nrequests.get(url, headers=headers) -
90% 成功率
from fastapi.security import HTTPBearer\nsecurity = HTTPBearer(auto_error=False)
无效尝试
常见但无效的做法:
-
80% 失败
Returns None token and downstream logic still fails.
-
85% 失败
OAuth2PasswordBearer expects the Authorization header by default.