invalid_grant api auth_error ai_generated true

OAuth2 错误:invalid_grant:授权码已过期

OAuth2 error: invalid_grant: Authorization code has expired

ID: api/oauth2-invalid-grant-authorization-code-expired

其他格式: JSON · Markdown 中文 · English
92%修复率
90%置信度
1证据数
2023-11-20首次发现

版本兼容性

版本状态引入弃用备注
OAuth 2.0 RFC 6749 active
Auth0 2024 active
Okta 2023 active
Keycloak 22.0 active

根因分析

令牌交换请求中使用的授权码已超过其允许的生命周期(通常为 60-600 秒)。

English

The authorization code used in the token exchange request was issued more than the allowed lifetime (typically 60-600 seconds) ago.

generic

官方文档

https://datatracker.ietf.org/doc/html/rfc6749#section-4.1.2

解决方案

  1. 在允许的时间窗口内(通常为 10 分钟)完成授权码交换。自动化重定向到令牌的流程,避免手动延迟。使用 PKCE 确保每次请求的 code_verifier 都是新鲜的。
  2. 如果使用基于浏览器的流程,确保回调端点立即触发令牌交换,无需用户交互导致延迟。

无效尝试

常见但无效的做法:

  1. 70% 失败

    The same user session may return the same expired code if the state parameter is reused; the root cause is timing, not the code value.

  2. 80% 失败

    Long-lived authorization codes violate OAuth 2.0 security best practices and may be rejected by client libraries; also the server may have a hard upper limit.