获取或刷新授权 Token 接口
POSThttps://gitcode.com/oauth/token
基本信息
- 请求路径:
/oauth/token
- 请求方法: POST
- 接口说明: 用于获取或刷新访问令牌(access_token)的统一接口
请求参数
Query 参数
参数名 | 必填 | 类型 | 说明 |
---|---|---|---|
grant_type | 是 | string | 授权类型。可选值: - authorization_code : 授权码模式,用于获取新token- refresh_token : 刷新token模式,用于刷新已有token |
code | 条件必填 | string | 授权码。当 grant_type=authorization_code 时必填 |
client_id | 条件必填 | string | 注册的客户端ID。当 grant_type=authorization_code 时必填 |
refresh_token | 条件必填 | string | 刷新令牌。当 grant_type=refresh_token 时必填 |
Body 参数
参数名 | 必填 | 类型 | 说明 |
---|---|---|---|
client_secret | 条件必填 | string | 注册的客户端密钥。当 grant_type=authorization_code 时必填 |
使用场景
- 获取新的访问令牌
- 设置 grant_type=authorization_code
- 提供 code、client_id 和 client_secret
- 刷新访问令牌
- 设置 grant_type=refresh_token
- 提供 refresh_token
示例
获取新token
POST /oauth/token?grant_type=authorization_code&code={code}&client_id={client_id}
Content-Type: application/json
{
"client_secret": "{client_secret}"
}
刷新token
POST /oauth/token?grant_type=refresh_token&refresh_token={refresh_token}
Request
Query Parameters
grant_type stringrequired
授权码模式
code string
授权码
client_id string
注册的客户端 ID
client_secret string
注册的客户端密钥
refresh_token string
刷新令牌,仅在 grant_type 为 refresh_token 时必传
- application/json
Body
client_secretstringrequired
注册的客户端密钥
Responses
- 200
成功响应
Response Headers
- application/json
- Schema
- Example (auto)
- 1
Schema
access_tokenstring
expires_ininteger
refresh_tokenstring
scopestring
created_atstring
{
"access_token": "string",
"expires_in": 0,
"refresh_token": "string",
"scope": "string",
"created_at": "string"
}
成功示例
{
"access_token": "eyPZPVNfsibj9tap_ibj3t3p",
"expires_in": 1296000,
"refresh_token": "b77ced3aee884348852160deab3697a1",
"scope": "all_user all_key all_groups all_projects all_pr all_issue all_note all_hook all_repository",
"created_at": "2024-04-20T09:07:59.889Z"
}
- csharp
- curl
- dart
- go
- http
- java
- javascript
- kotlin
- c
- nodejs
- objective-c
- ocaml
- php
- powershell
- python
- r
- ruby
- rust
- shell
- swift
- HTTPCLIENT
- RESTSHARP
var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Post, "https://gitcode.com/oauth/token");
request.Headers.Add("Accept", "application/json");
var content = new StringContent("{\n \"client_secret\": \"string\"\n}", null, "application/json");
request.Content = content;
var response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
Console.WriteLine(await response.Content.ReadAsStringAsync());
ResponseClear