{
  "id": "python/fastapi-oauth2-token-expired",
  "signature": "fastapi.exceptions.HTTPException: 401 Unauthorized: Token expired",
  "signature_zh": "FastAPI HTTP 异常：401 未授权：令牌已过期",
  "regex": "fastapi\\.exceptions\\.HTTPException:\\ 401\\ Unauthorized:\\ Token\\ expired",
  "domain": "python",
  "category": "auth_error",
  "subcategory": null,
  "root_cause": "OAuth2 令牌已过期，需要刷新",
  "root_cause_type": "generic",
  "root_cause_zh": "OAuth2 令牌已过期，需要刷新",
  "versions": [
    {
      "version": "3.9",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "3.10",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "忽略过期并继续使用旧令牌",
      "why_fails": "服务器会拒绝请求",
      "fail_rate": 0.95,
      "condition": "",
      "sources": []
    },
    {
      "action": "延长令牌有效期而不刷新",
      "why_fails": "安全风险增加",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "实现令牌刷新机制",
      "success_rate": 0.9,
      "how": "from fastapi.security import OAuth2PasswordBearer\noauth2_scheme = OAuth2PasswordBearer(tokenUrl='token', auto_error=False)\n@app.post('/refresh')\ndef refresh(token: str = Depends(oauth2_scheme)):\n    new_token = create_access_token(...)\n    return {'access_token': new_token}",
      "condition": "",
      "sources": []
    },
    {
      "action": "在客户端捕获 401 并刷新令牌",
      "success_rate": 0.85,
      "how": "response = requests.get(url, headers={'Authorization': f'Bearer {token}'})\nif response.status_code == 401:\n    token = refresh_token()\n    response = requests.get(url, headers={'Authorization': f'Bearer {token}'})",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": null,
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.84,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2025-10-15",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}