# 兑换或刷新 MCP OAuth 令牌

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

POST `/oauth/token`

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

## 请求参数

| 参数 | 类型 | 位置 | 必填 | 说明 |
| --- | --- | --- | --- | --- |
| `Authorization` | Basic &lt;client credentials&gt; | `header` | 否 | 注册为 client\_secret\_basic 时使用。 |
| `grant_type` | authorization\_code \| refresh\_token | `body` | 是 | 兑换授权码或刷新访问令牌。 |
| `client_id` | string | `body` | 否 | 公开客户端及 client\_secret\_post 认证必填。使用 HTTP Basic 时仅通过 Authorization 传递。 |
| `client_secret` | string | `body` | 否 | 注册为 client\_secret\_post 时必填。 |
| `resource` | URI | `body` | 是 | 当前服务绝对地址后加 /mcp。 |
| `scope` | mcp:tools | `body` | 否 | 接受 mcp:tools 范围。 |
| `code` | string | `body` | 否 | 授权码兑换时必填。 |
| `redirect_uri` | URI | `body` | 否 | 须与注册的回调地址一字不差。 |
| `code_verifier` | string | `body` | 否 | 授权码兑换时必填，43–128 字符。 |
| `refresh_token` | string | `body` | 否 | 刷新令牌时必填。 |

## 请求示例

**cURL**

```bash
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**

```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**

```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 · 示例响应**

```json
{
  "access_token": "<access token>",
  "token_type": "Bearer",
  "expires_in": 3600,
  "scope": "mcp:tools",
  "refresh_token": "<refresh token>"
}
```

**错误示例**

```json
{
  "error": "invalid_grant",
  "error_description": "The authorization grant is unavailable or invalid."
}
```

| 字段 | 类型 | 说明 |
| --- | --- | --- |
| `access_token` | string | 通过 Authorization Bearer 调用 MCP 的令牌。 |
| `token_type` | string | 访问令牌类型，值为 Bearer。 |
| `expires_in` | integer | 访问令牌的有效期（秒）。 |
| `scope` | string | 已授予的范围。 |
| `refresh_token` | string | 客户端注册 refresh\_token 授权时返回。 |

## 错误状态

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

## 使用细节

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

## 同组接口

-   [GET 检查数据源配置](<https://social2api.com/docs/api/status/>)
-   [POST 运行取数任务](<https://social2api.com/docs/api/run/>)
-   [POST 调用 MCP 服务](<https://social2api.com/docs/api/mcp/>)
-   [GET 读取 OAuth 授权服务元数据](<https://social2api.com/docs/api/oauth-server-metadata/>)
-   [GET 读取 OAuth 受保护资源元数据](<https://social2api.com/docs/api/oauth-resource-metadata/>)
-   [GET 读取 MCP 资源的 OAuth 元数据](<https://social2api.com/docs/api/oauth-mcp-metadata/>)
-   [POST 注册 MCP OAuth 客户端](<https://social2api.com/docs/api/oauth-register/>)
-   [GET 启动 MCP OAuth 授权](<https://social2api.com/docs/api/oauth-authorize-start/>)
-   [POST 提交 MCP OAuth 授权决定](<https://social2api.com/docs/api/oauth-authorize-consent/>)

规范页面：<https://social2api.com/docs/api/oauth-token/>
