令牌端点

2026-11-22 업데이트
On this page

换 token

POST /oauth/token,请求体是 application/x-www-form-urlencoded。客户端认证可以放在请求体里,也可以用 HTTP Basic。

POST https://id.caellab.com/oauth/token
Content-Type: application/x-www-form-urlencoded

grant_type=authorization_code
&code=AUTHORIZATION_CODE
&redirect_uri=YOUR_CALLBACK_URL
&client_id=YOUR_CLIENT_ID
&client_secret=YOUR_CLIENT_SECRET
&code_verifier=YOUR_CODE_VERIFIER
  • 机密客户端:client_secret 必填。

  • 公共客户端:不传 client_secret,只传 client_id 和 code_verifier。

  • 授权时带了 code_challenge 的,换 token 必须带对应的 code_verifier。

  • redirect_uri 要和授权时用的一致。

  • 授权码只能换一次,换完就删掉。

成功返回:

{
  "access_token": "…",
  "token_type": "Bearer",
  "expires_in": 3600,
  "refresh_token": "…",
  "scope": "openid profile"
}

刷新 token

POST https://id.caellab.com/oauth/token
Content-Type: application/x-www-form-urlencoded

grant_type=refresh_token
&refresh_token=YOUR_REFRESH_TOKEN
&client_id=YOUR_CLIENT_ID
&client_secret=YOUR_CLIENT_SECRET

每次刷新都会发一对新的 access_token 和 refresh_token,旧的 refresh_token 立刻失效,旧的 access_token 也一起作废。新的 refresh_token 有效期重新按 180 天算,记得把两个都存回去。

撤销 token

POST https://id.caellab.com/oauth/revoke
Content-Type: application/x-www-form-urlencoded

token=THE_TOKEN

access_token 和 refresh_token 都可以传,按哈希值查两张表。不管有没有命中都返回 200 和 {},不用靠状态码判断结果。

客户端认证

  • 请求体里的 client_id + client_secret,或 HTTP Basic(用户名是 client_id,密码是 client_secret)。

  • 公共客户端不校验 client_secret,换 token 时靠 PKCE 兜底。

  • client_secret 只在创建应用时显示一次;重置以后旧密钥立即失效,该应用所有活跃 token 也会被撤销。

错误

情况errorHTTP
client_id 或 client_secret 不对invalid_client401
缺少必要参数invalid_request400
授权码无效、过期、已用过invalid_grant400
redirect_uri 和授权时不一致invalid_grant400
code_verifier 不匹配或格式不对invalid_grant400
强制 PKCE 的应用拿到没绑 challenge 的码invalid_grant400
刷新 token 无效、过期、已撤销invalid_grant400
grant_type 不是上面两种unsupported_grant_type400
超过频率限制rate_limited429

错误响应格式:

{
  "error": "invalid_grant",
  "error_description": "Authorization code invalid or expired"
}

/oauth/token 按请求方 IP 限流,30 次/分钟。超限返回 429,响应头带 Retry-After。