# fastapi.exceptions.HTTPException: 401 Unauthorized - Invalid token

- **ID:** `python/fastapi-oauth-error`
- **Domain:** python
- **Category:** auth_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

OAuth2 令牌无效或过期，FastAPI 无法验证用户身份。

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 3.x | active | — | — |

## Workarounds

1. **** (90% success)
   ```
   实现令牌刷新机制，使用 refresh_token。
   ```
2. **** (85% success)
   ```
   检查令牌过期时间，提前刷新。
   ```
3. **** (95% success)
   ```
   使用 JWT 验证库确保令牌正确。
   ```

## Dead Ends

- **** — 忽略错误，不处理令牌刷新。 (80% fail)
- **** — 尝试在前端硬编码令牌，但会过期。 (70% fail)
- **** — 修改密钥但忘记更新客户端。 (60% fail)
