兑换或刷新 MCP OAuth 令牌

用授权码与 PKCE 校验值兑换访问令牌,或用已有刷新令牌续期。

POST/oauth/token
认证
按注册设置发送公开客户端 ID、HTTP Basic 或表单形式的客户端凭据。
成功状态
200

请求参数

参数类型位置必填说明
AuthorizationBasic <client credentials>header否注册为 client_secret_basic 时使用。
grant_typeauthorization_code | refresh_tokenbody是兑换授权码或刷新访问令牌。
client_idstringbody否公开客户端及 client_secret_post 认证必填。使用 HTTP Basic 时仅通过 Authorization 传递。
client_secretstringbody否注册为 client_secret_post 时必填。
resourceURIbody是当前服务绝对地址后加 /mcp。
scopemcp:toolsbody否接受 mcp:tools 范围。
codestringbody否授权码兑换时必填。
redirect_uriURIbody否须与注册的回调地址一字不差。
code_verifierstringbody否授权码兑换时必填,43–128 字符。
refresh_tokenstringbody否刷新令牌时必填。

请求示例

cURL
curl -i -X POST 'https://social2api.com/oauth/token' \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  --data-binary 'grant_type=authorization_code&client_id=client_4a5076aa-f9ab-46e6-b383-7b1374392dc4&code=%3Cauthorization+code%3E&redirect_uri=https%3A%2F%2Fexample.com%2Foauth%2Fcallback&code_verifier=dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk&resource=https%3A%2F%2Fsocial2api.com%2Fmcp'
Python
from urllib.error import HTTPError
from urllib.request import Request, urlopen

request = Request(
    "https://social2api.com/oauth/token",
    method="POST",
    headers={
        "Content-Type": "application/x-www-form-urlencoded",
    },
    data="grant_type=authorization_code&client_id=client_4a5076aa-f9ab-46e6-b383-7b1374392dc4&code=%3Cauthorization+code%3E&redirect_uri=https%3A%2F%2Fexample.com%2Foauth%2Fcallback&code_verifier=dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk&resource=https%3A%2F%2Fsocial2api.com%2Fmcp".encode("utf-8"),
)
send = urlopen
try:
    with send(request) as response:
        print(response.status, response.read().decode("utf-8"))
except HTTPError as error:
    print(error.code, error.read().decode("utf-8"))
JavaScript
const response = await fetch("/oauth/token", {
  method: "POST",
  headers: {
    "Content-Type": "application/x-www-form-urlencoded",
  },
  body: new URLSearchParams({
    "grant_type": "authorization_code",
    "client_id": "client_4a5076aa-f9ab-46e6-b383-7b1374392dc4",
    "code": "<authorization code>",
    "redirect_uri": "https://example.com/oauth/callback",
    "code_verifier": "dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk",
    "resource": "https://social2api.com/mcp"
  }),
});
console.log(response.status, await response.text());

响应

200 · 示例响应
{
  "access_token": "<access token>",
  "token_type": "Bearer",
  "expires_in": 3600,
  "scope": "mcp:tools",
  "refresh_token": "<refresh token>"
}
错误示例
{
  "error": "invalid_grant",
  "error_description": "The authorization grant is unavailable or invalid."
}
字段类型说明
access_tokenstring通过 Authorization Bearer 调用 MCP 的令牌。
token_typestring访问令牌类型,值为 Bearer。
expires_ininteger访问令牌的有效期(秒)。
scopestring已授予的范围。
refresh_tokenstring客户端注册 refresh_token 授权时返回。

错误状态

HTTP原因与处理
400OAuth 请求、范围、资源或表单参数无效。
401客户端凭据无效,返回 invalid_client。
405请求方法不受支持。
500授权服务暂时不可用。
429请求过多。等待 Retry-After 指定秒数后重试。

使用细节

  • 兑换授权码时必须提供原注册 redirect_uri、原 PKCE 校验值和回调返回的 code。使用刷新令牌时须保存每次返回的替代令牌。
  • 访问令牌 1 小时后过期,刷新令牌有效期 30 天。表单体最多 8,192 字节,每 IP 每分钟最多 90 次请求。