{
  "id": "security/oauth2-token-endpoint-http-400",
  "signature": "OAuth2 token endpoint returned HTTP 400: invalid_grant with error_description 'Invalid authorization code'",
  "signature_zh": "OAuth2令牌端点返回HTTP 400：invalid_grant，错误描述为'无效的授权码'",
  "regex": "OAuth2 token endpoint returned HTTP 400: invalid_grant with error_description 'Invalid authorization code'",
  "domain": "security",
  "category": "protocol_error",
  "subcategory": null,
  "root_cause": "The authorization code used in the token exchange is invalid, expired, or has already been used, often due to a race condition where multiple requests attempt to use the same code or the code was issued for a different client ID.",
  "root_cause_type": "generic",
  "root_cause_zh": "令牌交换中使用的授权码无效、已过期或已被使用，通常是由于多个请求尝试使用同一个授权码的竞态条件，或者该授权码是为不同的客户端ID签发的。",
  "versions": [
    {
      "version": "Spring Security 6.0.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Passport.js 0.6.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Okta SDK 1.0.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Express OAuth2 Server 3.0.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Retrying the token exchange immediately with the same authorization code will always fail because the code is single-use and has been consumed on the first attempt.",
      "fail_rate": 1.0,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Increasing the authorization code expiration time on the server can help with delays but does not fix the root cause if the code is being reused or mismatched.",
      "fail_rate": 0.4,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Clearing browser cookies and restarting the flow may work but is not a systematic fix; it only masks the issue for the user and doesn't address the underlying client-side bug.",
      "fail_rate": 0.5,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Ensure the authorization code is used exactly once. If the token exchange fails due to a network error, do not automatically retry with the same code; instead, redirect the user to re-authenticate.",
      "success_rate": 0.9,
      "how": "Ensure the authorization code is used exactly once. If the token exchange fails due to a network error, do not automatically retry with the same code; instead, redirect the user to re-authenticate.",
      "condition": "",
      "sources": []
    },
    {
      "action": "Check that the client ID and redirect URI used in the token request exactly match those used in the authorization request. Mismatches cause the code to be rejected.",
      "success_rate": 0.8,
      "how": "Check that the client ID and redirect URI used in the token request exactly match those used in the authorization request. Mismatches cause the code to be rejected.",
      "condition": "",
      "sources": []
    },
    {
      "action": "If the code is being consumed by a background job or multiple workers, add a lock or a state machine to ensure only one request can exchange the code at a time.",
      "success_rate": 0.75,
      "how": "If the code is being consumed by a background job or multiple workers, add a lock or a state machine to ensure only one request can exchange the code at a time.",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "确保授权码只使用一次。如果令牌交换因网络错误失败，不要自动使用相同的授权码重试；而是将用户重定向到重新认证。",
    "检查令牌请求中使用的客户端ID和重定向URI是否与授权请求中使用的完全匹配。不匹配会导致授权码被拒绝。",
    "如果授权码被后台任务或多个工作进程消费，添加锁或状态机以确保同一时间只有一个请求可以交换该授权码。"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://datatracker.ietf.org/doc/html/rfc6749#section-5.2",
  "official_doc_section": null,
  "error_code": "OAUTH2_INVALID_GRANT",
  "verification_tier": "ai_generated",
  "confidence": 0.87,
  "fix_success_rate": 0.8,
  "resolvable": "partial",
  "first_seen": "2023-08-10",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}