跳到主要内容

获取或刷新授权 Token 接口

POST 

https://gitcode.com/oauth/token

基本信息

  • 请求路径: /oauth/token
  • 请求方法: POST
  • 接口说明: 用于获取或刷新访问令牌(access_token)的统一接口

请求参数

Query 参数

参数名必填类型说明
grant_typestring授权类型。可选值:
- 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 时必填

使用场景

  1. 获取新的访问令牌
    • 设置 grant_type=authorization_code
    • 提供 code、client_id 和 client_secret
  2. 刷新访问令牌
    • 设置 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 时必传

Body

    client_secretstringrequired

    注册的客户端密钥

Responses

成功响应

Response Headers
    Schema
      access_tokenstring
      expires_ininteger
      refresh_tokenstring
      scopestring
      created_atstring
    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());
    Request Collapse all
    Base URL
    https://gitcode.com
    Parameters
    — queryrequired
    — query
    — query
    — query
    — query
    Body
    {
      "client_secret": "string"
    }
    
    ResponseClear

    Click the Send API Request button above and see the response here!