Help us learn about your current experience with the documentation. Take the survey.

项目访问令牌 API

  • Tier: 免费版、高级版、旗舰版
  • Offering: GitLab.com、GitLab 自托管、GitLab 专用版

使用此 API 与项目访问令牌进行交互。更多信息,请参阅项目访问令牌

列出所有项目访问令牌

列出指定项目的所有项目访问令牌。

GET projects/:id/access_tokens
GET projects/:id/access_tokens?state=inactive
属性 类型 必需 描述
id integer or string 项目的 ID 或 URL 编码路径
created_after datetime (ISO 8601) 如果已定义,则返回在指定时间之后创建的令牌。
created_before datetime (ISO 8601) 如果已定义,则返回在指定时间之前创建的令牌。
expires_after date (ISO 8601) 如果已定义,则返回在指定时间之后过期的令牌。
expires_before date (ISO 8601) 如果已定义,则返回在指定时间之前过期的令牌。
last_used_after datetime (ISO 8601) 如果已定义,则返回在指定时间之后最后使用的令牌。
last_used_before datetime (ISO 8601) 如果已定义,则返回在指定时间之前最后使用的令牌。
revoked boolean 如果为 true,则只返回已撤销的令牌。
search string 如果已定义,则返回名称中包含指定值的令牌。
sort string 如果已定义,则按指定值对结果进行排序。可能的值:created_asccreated_descexpires_ascexpires_desclast_used_asclast_used_descname_ascname_desc
state string 如果已定义,则返回具有指定状态的令牌。可能的值:activeinactive
curl --request GET \
  --header "PRIVATE-TOKEN: <your_access_token>" \
  --url "https://gitlab.example.com/api/v4/projects/<project_id>/access_tokens"
[
   {
      "user_id" : 141,
      "scopes" : [
         "api"
      ],
      "name" : "token",
      "expires_at" : "2021-01-31",
      "id" : 42,
      "active" : true,
      "created_at" : "2021-01-20T22:11:48.151Z",
      "description": "Test Token description",
      "last_used_at" : null,
      "revoked" : false,
      "access_level" : 40
   },
   {
      "user_id" : 141,
      "scopes" : [
         "read_api"
      ],
      "name" : "token-2",
      "expires_at" : "2021-01-31",
      "id" : 43,
      "active" : false,
      "created_at" : "2021-01-21T12:12:38.123Z",
      "description": "Test Token description",
      "revoked" : true,
      "last_used_at" : "2021-02-13T10:34:57.178Z",
      "access_level" : 40
   }
]

获取项目访问令牌的详细信息

获取项目访问令牌的详细信息。

GET projects/:id/access_tokens/:token_id
属性 类型 必需 描述
id integer or string 项目的 ID 或 URL 编码路径
token_id integer or string ID
curl --request GET \
  --header "PRIVATE-TOKEN: <your_access_token>" \
  --url "https://gitlab.example.com/api/v4/projects/<project_id>/access_tokens/<token_id>"
{
   "user_id" : 141,
   "scopes" : [
      "api"
   ],
   "name" : "token",
   "expires_at" : "2021-01-31",
   "id" : 42,
   "active" : true,
   "created_at" : "2021-01-20T22:11:48.151Z",
   "description": "Test Token description",
   "revoked" : false,
   "access_level": 40,
   "last_used_at": "2022-03-15T11:05:42.437Z"
}

创建项目访问令牌

为指定项目创建项目访问令牌。您无法创建访问级别高于您账户的令牌。例如,具有维护者角色的用户无法创建具有所有者角色的项目访问令牌。

您必须使用个人访问令牌访问此端点。您无法使用项目访问令牌进行身份验证。有一个开放的功能请求来添加此功能。

POST projects/:id/access_tokens
属性 类型 必需 描述
id integer or string 项目的 ID 或 URL 编码路径
name string 令牌的名称。
description string 项目访问令牌的描述。
scopes Array[String] 令牌可用的范围列表。
access_level integer 令牌的角色。可能的值:10(访客)、15(规划者)、20(报告者)、30(开发者)、40(维护者)和 50(所有者)。默认值:40
expires_at date 令牌的过期日期,采用 ISO 格式(YYYY-MM-DD)。如果未定义,则日期设置为最大允许生命周期限制
curl --request POST \
  --header "PRIVATE-TOKEN: <your_personal_access_token>" \
  --header "Content-Type:application/json" \
  --data '{ "name":"test_token", "scopes":["api", "read_repository"], "expires_at":"2021-01-31", "access_level":30 }' \
  --url "https://gitlab.example.com/api/v4/projects/<project_id>/access_tokens"
{
   "scopes" : [
      "api",
      "read_repository"
   ],
   "active" : true,
   "name" : "test",
   "revoked" : false,
   "created_at" : "2021-01-21T19:35:37.921Z",
   "description": "Test Token description",
   "user_id" : 166,
   "id" : 58,
   "expires_at" : "2021-01-31",
   "token" : "D4y...Wzr",
   "access_level": 30
}

轮换项目访问令牌

轮换项目访问令牌。这会立即撤销之前的令牌并创建一个新令牌。通常,此端点通过使用个人访问令牌进行身份验证来轮换特定的项目访问令牌。您也可以使用项目访问令牌轮换自身。更多信息,请参阅自我轮换

如果您尝试使用此端点轮换先前已撤销的令牌,则同一令牌族中的任何活动令牌都将被撤销。更多信息,请参阅自动重用检测

先决条件:

POST /projects/:id/access_tokens/:token_id/rotate
属性 类型 必需 描述
id integer or string 项目的 ID 或 URL 编码路径
token_id integer or string 项目访问令牌的 ID 或关键字 self
expires_at date 访问令牌的过期日期,采用 ISO 格式(YYYY-MM-DD)。如果令牌需要过期日期,则默认为 1 周。如果不要求,则默认为最大允许生命周期限制
curl --request POST \
  --header "PRIVATE-TOKEN: <your_access_token>" \
  --url "https://gitlab.example.com/api/v4/projects/<project_id>/access_tokens/<token_id>/rotate"

示例响应:

{
    "id": 42,
    "name": "Rotated Token",
    "revoked": false,
    "created_at": "2023-08-01T15:00:00.000Z",
    "description": "Test project access token",
    "scopes": ["api"],
    "user_id": 1337,
    "last_used_at": null,
    "active": true,
    "expires_at": "2023-08-15",
    "access_level": 30,
    "token": "s3cr3t"
}

如果成功,返回 200: OK

其他可能的响应:

  • 400: Bad Request - 如果轮换不成功。
  • 401: Unauthorized - 如果满足以下任一条件:
    • 令牌不存在。
    • 令牌已过期。
    • 令牌已被撤销。
    • 您没有权限访问指定的令牌。
    • 您正在使用项目访问令牌轮换另一个项目访问令牌。请改用自我轮换
  • 403: Forbidden - 如果令牌不允许自我轮换。
  • 404: Not Found - 如果用户是管理员但令牌不存在。
  • 405: Method Not Allowed - 如果令牌不是项目访问令牌。

自我轮换

您不是轮换特定的项目访问令牌,而是轮换用于请求身份验证的同一项目访问令牌。要进行自我轮换,您必须:

示例请求:

curl --request POST \
  --header "PRIVATE-TOKEN: <your_project_access_token>" \
  --url "https://gitlab.example.com/api/v4/projects/<project_id>/access_tokens/self/rotate"

撤销项目访问令牌

撤销指定的项目访问令牌。

DELETE projects/:id/access_tokens/:token_id
属性 类型 必需 描述
id integer or string 项目的 ID 或 URL 编码路径
token_id integer 项目访问令牌的 ID。
curl --request DELETE \
  --header "PRIVATE-TOKEN: <your_access_token>" \
  --url "https://gitlab.example.com/api/v4/projects/<project_id>/access_tokens/<token_id>"

如果成功,返回 204 No content

其他可能的响应:

  • 400: Bad Request - 如果撤销不成功。
  • 404: Not Found - 如果访问令牌不存在。