项目访问令牌 API
- Tier: 基础版,专业版,旗舰版
- Offering: JihuLab.com,私有化部署
使用此 API 与项目访问令牌交互。有关更多信息,请参阅项目访问令牌。
列出所有项目访问令牌
History
- state 属性在极狐GitLab 17.2 中引入。
列出指定项目的所有项目访问令牌。
plaintextGET projects/:id/access_tokens GET projects/:id/access_tokens?state=inactive
| 属性 | 类型 | 必需 | 描述 |
|---|---|---|---|
| id | integer 或 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_asc,created_desc,expires_asc,expires_desc,last_used_asc,last_used_desc,name_asc,name_desc。 |
| state | string | 否 | 如果已定义,返回具有指定状态的令牌。可能的值:active 和 inactive。 |
shellcurl --request GET \ --header "PRIVATE-TOKEN: <your_access_token>" \ --url "https://gitlab.example.com/api/v4/projects/<project_id>/access_tokens"
json1[ 2 { 3 "user_id" : 141, 4 "scopes" : [ 5 "api" 6 ], 7 "name" : "token", 8 "expires_at" : "2021-01-31", 9 "id" : 42, 10 "active" : true, 11 "created_at" : "2021-01-20T22:11:48.151Z", 12 "description": "Test Token description", 13 "last_used_at" : null, 14 "revoked" : false, 15 "access_level" : 40 16 }, 17 { 18 "user_id" : 141, 19 "scopes" : [ 20 "read_api" 21 ], 22 "name" : "token-2", 23 "expires_at" : "2021-01-31", 24 "id" : 43, 25 "active" : false, 26 "created_at" : "2021-01-21T12:12:38.123Z", 27 "description": "Test Token description", 28 "revoked" : true, 29 "last_used_at" : "2021-02-13T10:34:57.178Z", 30 "access_level" : 40 31 } 32]
获取项目访问令牌的详细信息
获取项目访问令牌的详细信息。您可以引用特定的项目访问令牌,或者使用关键字 self 返回身份验证项目访问令牌的详细信息。
plaintextGET projects/:id/access_tokens/:token_id
| 属性 | 类型 | 必需 | 描述 |
|---|---|---|---|
| id | integer 或 string | 是 | 项目的 ID 或URL 编码路径。 |
| token_id | integer 或 string | 是 | 项目访问令牌的 ID 或关键字 self。 |
shellcurl --request GET \ --header "PRIVATE-TOKEN: <your_access_token>" \ --url "https://gitlab.example.com/api/v4/projects/<project_id>/access_tokens/<token_id>"
json1{ 2 "user_id" : 141, 3 "scopes" : [ 4 "api" 5 ], 6 "name" : "token", 7 "expires_at" : "2021-01-31", 8 "id" : 42, 9 "active" : true, 10 "created_at" : "2021-01-20T22:11:48.151Z", 11 "description": "Test Token description", 12 "revoked" : false, 13 "access_level": 40, 14 "last_used_at": "2022-03-15T11:05:42.437Z" 15}
创建项目访问令牌
History
- 在极狐GitLab 16.0 中引入了 expires_at 属性的默认值。
为指定项目创建项目访问令牌。您无法创建访问级别高于您账户的令牌。例如,具有维护者角色的用户无法创建具有所有者角色的项目访问令牌。
您必须使用个人访问令牌进行身份验证。无法使用项目访问令牌进行身份验证。对此功能有一个开放功能请求。
plaintextPOST projects/:id/access_tokens
shellcurl --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"
json1{ 2 "scopes" : [ 3 "api", 4 "read_repository" 5 ], 6 "active" : true, 7 "name" : "test", 8 "revoked" : false, 9 "created_at" : "2021-01-21T19:35:37.921Z", 10 "description": "Test Token description", 11 "user_id" : 166, 12 "id" : 58, 13 "expires_at" : "2021-01-31", 14 "token" : "D4y...Wzr", 15 "access_level": 30 16}
旋转项目访问令牌
History
- 在极狐GitLab 16.0 中引入。
- 在极狐GitLab 16.6 中添加了 expires_at 属性。
旋转项目访问令牌。这会立即撤销以前的令牌并创建新令牌。通常,此端点通过使用个人访问令牌进行身份验证来旋转特定项目访问令牌。您还可以使用项目访问令牌自旋。有关更多信息,请参阅自旋。
如果尝试使用此端点旋转以前已撤销的令牌,则同一令牌系列中的任何活动令牌都会被撤销。有关更多信息,请参阅自动重用检测。
先决条件:
- 具有 api 范围的个人访问令牌或具有 api 或 self_rotate 范围的项目访问令牌。请参阅自旋。
plaintextPOST /projects/:id/access_tokens/:token_id/rotate
| 属性 | 类型 | 必需 | 描述 |
|---|---|---|---|
| id | integer 或 string | 是 | 项目的 ID 或URL 编码路径。 |
| token_id | integer 或 string | 是 | 项目访问令牌的 ID 或关键字 self。 |
| expires_at | date | 否 | 访问令牌的过期日期,以 ISO 格式 (YYYY-MM-DD) 表示。日期必须在旋转日期的一年或更短时间内。如果未定义,则令牌在一周后过期。 |
shellcurl --request POST \ --header "PRIVATE-TOKEN: <your_access_token>" \ --url "https://gitlab.example.com/api/v4/projects/<project_id>/access_tokens/<token_id>/rotate"
示例响应:
json1{ 2 "id": 42, 3 "name": "Rotated Token", 4 "revoked": false, 5 "created_at": "2023-08-01T15:00:00.000Z", 6 "description": "Test project access token", 7 "scopes": ["api"], 8 "user_id": 1337, 9 "last_used_at": null, 10 "active": true, 11 "expires_at": "2023-08-15", 12 "access_level": 30, 13 "token": "s3cr3t" 14}
如果成功,则返回 200: OK。
其他可能的响应:
- 400: Bad Request 如果没有成功旋转。
- 401: Unauthorized 如果以下任一条件为真:
- 令牌不存在。
- 令牌已过期。
- 令牌已被撤销。
- 您没有访问指定令牌的权限。
- 您正在使用项目访问令牌旋转另一个项目访问令牌。请改为参阅自轮换。
- 403: Forbidden 如果令牌不允许自旋。
- 404: Not Found 如果用户是管理员但令牌不存在。
- 405: Method Not Allowed 如果令牌不是项目访问令牌。
自旋
您可以旋转用于验证请求的相同项目访问令牌,而不是旋转特定项目访问令牌。要自旋项目访问令牌,您必须:
- 旋转具有 api 或 self_rotate 范围的项目访问令牌。
- 在请求 URL 中使用 self 关键字。
示例请求:
shellcurl --request POST \ --header "PRIVATE-TOKEN: <your_project_access_token>" \ --url "https://gitlab.example.com/api/v4/projects/<project_id>/access_tokens/self/rotate"
撤销项目访问令牌
撤销指定的项目访问令牌。
plaintextDELETE projects/:id/access_tokens/:token_id
| 属性 | 类型 | 必需 | 描述 |
|---|---|---|---|
| id | integer 或 string | 是 | 项目的 ID 或URL 编码路径。 |
| token_id | integer | 是 | 项目访问令牌的 ID。 |
shellcurl --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 如果访问令牌不存在。