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

- **ID:** `python/fastapi-oauth2-token-401`
- **领域:** python
- **类别:** auth_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

OAuth2PasswordBearer(tokenUrl=...) 未收到 Authorization 头，或 tokenUrl 路径与实际登录路由不匹配。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 0.100+ | active | — | — |
| 0.110+ | active | — | — |

## 解决方案

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

## 无效尝试

- **** — Token becomes None; downstream still fails auth. (70% 失败率)
- **** — Security regression; endpoints become public. (95% 失败率)
