# 启动 MCP OAuth 授权

浏览器验证客户端、回调、请求资源与 PKCE。提交成功后进入登录或授权确认。

GET `/oauth/authorize`

- **认证** 可选控制台会话 Cookie。
- **成功状态** `303, 200`

## 请求参数

| 参数 | 类型 | 位置 | 必填 | 说明 |
| --- | --- | --- | --- | --- |
| `client_id` | string | `query` | 是 | 动态注册得到的客户端标识。 |
| `redirect_uri` | URI | `query` | 是 | 须与注册的回调地址一字不差。 |
| `response_type` | code | `query` | 是 | 授权响应类型。 |
| `code_challenge_method` | S256 | `query` | 是 | PKCE 的哈希方式。 |
| `code_challenge` | string | `query` | 是 | 校验值的 SHA-256 摘要，使用不带填充的 Base64URL。 |
| `resource` | URI | `query` | 是 | 当前服务绝对地址后加 /mcp。 |
| `scope` | mcp:tools | `query` | 否 | 接受 mcp:tools 范围。 |
| `state` | string | `query` | 否 | 客户端设置的随机状态值，最多 1,024 字符，授权结果原样返回。 |
| `request` | string | `query` | 否 | 从 Location 收到的临时申请值。查看授权页时只传此值。 |

## 请求示例

**cURL**

```bash
curl -i -X GET 'https://social2api.com/oauth/authorize?client_id=client_4a5076aa-f9ab-46e6-b383-7b1374392dc4&redirect_uri=https%3A%2F%2Fexample.com%2Foauth%2Fcallback&response_type=code&code_challenge_method=S256&code_challenge=E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM&resource=https%3A%2F%2Fsocial2api.com%2Fmcp&scope=mcp%3Atools&state=%3Cclient+state%3E'
```

**Python**

```python
from urllib.error import HTTPError
from urllib.request import Request, urlopen

request = Request(
    "https://social2api.com/oauth/authorize?client_id=client_4a5076aa-f9ab-46e6-b383-7b1374392dc4&redirect_uri=https%3A%2F%2Fexample.com%2Foauth%2Fcallback&response_type=code&code_challenge_method=S256&code_challenge=E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM&resource=https%3A%2F%2Fsocial2api.com%2Fmcp&scope=mcp%3Atools&state=%3Cclient+state%3E",
    method="GET",
)
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/authorize?client_id=client_4a5076aa-f9ab-46e6-b383-7b1374392dc4&redirect_uri=https%3A%2F%2Fexample.com%2Foauth%2Fcallback&response_type=code&code_challenge_method=S256&code_challenge=E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM&resource=https%3A%2F%2Fsocial2api.com%2Fmcp&scope=mcp%3Atools&state=%3Cclient+state%3E", {
  method: "GET",
});
console.log(response.status, await response.text());
```

## 响应

**303 · 示例响应**

无响应正文

**200 · HTML**

```html
<form action="/oauth/authorize" method="post">…</form>
```

**错误示例**

```json
{
  "error": "invalid_request",
  "error_description": "The client or exact redirect URI is not registered."
}
```

`request=%3Cauthorization+request%3E` · `HTTP 200` · 已登录后读取待确认申请，返回浏览器授权页面。

| 字段 | 类型 | 说明 |
| --- | --- | --- |
| `Location` | URI reference | 开始时指向待确认申请。未登录时指向登录页，登录后返回待确认申请。 |

## 错误状态

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

## 使用细节

-   在浏览器中打开授权地址，使用本次申请的 PKCE 校验值。临时申请有效期 15 分钟。每 IP 每分钟最多发起 40 次。

## 同组接口

-   [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/>)
-   [POST 提交 MCP OAuth 授权决定](<https://social2api.com/docs/api/oauth-authorize-consent/>)
-   [POST 兑换或刷新 MCP OAuth 令牌](<https://social2api.com/docs/api/oauth-token/>)

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