python auth_error ai_generated true

fastapi.exceptions.HTTPException: 401 未授权:未认证

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

ID: python/fastapi-oauth2-missing-token

其他格式: JSON · Markdown 中文 · English
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.

generic

解决方案

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

无效尝试

常见但无效的做法:

  1. 80% 失败

    Returns None token and downstream logic still fails.

  2. 85% 失败

    OAuth2PasswordBearer expects the Authorization header by default.