python auth_error ai_generated true

fastapi.exceptions.HTTPException: 401 未授权:{"detail":"未认证"}

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

ID: python/fastapi-oauth2-token-missing

其他格式: JSON · Markdown 中文 · English
80%修复率
88%置信度
0证据数
2024-06-28首次发现

版本兼容性

版本状态引入弃用备注
0.100+ active — — —

根因分析

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

English

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

generic

解决方案

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

无效尝试

常见但无效的做法:

  1. 80% 失败

    Route then receives None token and crashes on decode.

  2. 90% 失败

    Auth bypass ships to production; security hole.