python auth_error ai_generated true

HTTPException 401: {"detail":"未认证"}

HTTPException 401: {"detail":"Not authenticated"}

ID: python/fastapi-oauth2-token-401

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

generic

解决方案

  1. 92% 成功率
    oauth2_scheme = OAuth2PasswordBearer(tokenUrl='/auth/token')  # must match @app.post('/auth/token')
  2. 93% 成功率
    Client: headers={'Authorization': f'Bearer {token}'}

无效尝试

常见但无效的做法:

  1. 70% 失败

    Token becomes None; downstream still fails auth.

  2. 95% 失败

    Security regression; endpoints become public.