OAuth
OAuth 2.0 接口用于第三方应用授权。
授权端点
GET https://ai-tokenhub.com/oauth/authorize请求参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| client_id | string | 是 | 客户端 ID |
| redirect_uri | string | 是 | 回调地址 |
| response_type | string | 是 | 授权类型 (code) |
| scope | string | 否 | 权限范围 |
| state | string | 否 | 状态参数 |
令牌端点
POST https://ai-tokenhub.com/oauth/token请求体
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| grant_type | string | 是 | 授权类型 |
| code | string | 是 | 授权码 |
| client_id | string | 是 | 客户端 ID |
| client_secret | string | 是 | 客户端密钥 |
| redirect_uri | string | 是 | 回调地址 |
授权码模式流程
1. 请求授权
https://ai-tokenhub.com/oauth/authorize?
client_id=YOUR_CLIENT_ID&
redirect_uri=https://yourapp.com/callback&
response_type=code&
scope=read:user%20write:api&
state=random_state_string2. 用户授权后回调
https://yourapp.com/callback?code=AUTH_CODE&state=random_state_string3. 获取访问令牌
bash
curl -X POST https://ai-tokenhub.com/oauth/token \
-H "Content-Type: application/x-www-form-urlencoded" \
-d '{
"grant_type": "authorization_code",
"code": "AUTH_CODE",
"client_id": "YOUR_CLIENT_ID",
"client_secret": "YOUR_CLIENT_SECRET",
"redirect_uri": "https://yourapp.com/callback"
}'响应示例
json
{
"access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"token_type": "Bearer",
"expires_in": 3600,
"refresh_token": "dGhpcyBpcyBhIHJlZnJlc2ggdG9rZW4...",
"scope": "read:user write:api"
}刷新令牌
bash
curl -X POST https://ai-tokenhub.com/oauth/token \
-H "Content-Type: application/x-www-form-urlencoded" \
-d '{
"grant_type": "refresh_token",
"refresh_token": "REFRESH_TOKEN",
"client_id": "YOUR_CLIENT_ID",
"client_secret": "YOUR_CLIENT_SECRET"
}'Scope 权限范围
| Scope | 说明 |
|---|---|
| read:user | 读取用户信息 |
| write:user | 修改用户信息 |
| read:api | 读取 API 使用量 |
| write:api | 管理 API 密钥 |
| read:billing | 读取账单信息 |
| write:billing | 管理充值和订阅 |