找不到基础 GitLab API 基础 url

问题描述 投票:0回答:3

我已经在我的 gitlab.com 帐户上创建了项目和存储库,生成了私钥,现在我正在尝试进行 api 调用来获取提交列表。

现在我想通过 api 从文档中获取项目列表 https://docs.gitlab.com/ce/api/projects.html#list-projects

GET /projects

所以我正在做

curl --header "PRIVATE-TOKEN: XXXXXXX -c" "https://gitlab.com/projects"

并得到 404。 我尝试了几种组合,但找不到正确的基本网址。

存储库提交、文档也是如此https://docs.gitlab.com/ce/api/commits.html

https://gitlab.example.com/api/v3/projects/5/repository/commits

很好,我正在尝试(使用 myusername/projectname 作为项目 id)

https://gitlab.com/api/v3/projects/myusername/projectname/repository/commits

也得到了404

api gitlab
3个回答
61
投票

托管 GitLab 的正确基本 URL 是

https://gitlab.com/api/v4/
,因此您的请求为
GET /projects
将会是

curl --header "PRIVATE-TOKEN: XXXXXX" "https://gitlab.com/api/v4/projects"

这将返回您可见的所有项目,包括其他用户的公共项目

如果您只想查看您的项目,那么您应该使用

GET /users/:user_id/projects
端点,其中
:user_id
是您的用户 ID,可以在您的 GitLab 个人资料页面 或对您的请求的响应中找到
GET /user
如果您已通过身份验证。

# Get :user_id from this request
curl --header "PRIVATE-TOKEN: XXXXXX" "https://gitlab.com/api/v4/user"

# See your projects by replacing :user_id with id value from previous request
curl --header "PRIVATE-TOKEN: XXXXXX" "https://gitlab.com/api/v4/users/:user_id/projects"

此外,项目 ID 与项目名称不同。您可以从您对

GET /users/:user_id/projects
的请求的响应中或从项目的设置页面中检索项目 ID。


10
投票

对我来说以下请求有效:

curl --header "PRIVATE-TOKEN: YOUR_TOKEN" "https://gitlab.com/api/v4/users/YOUR_USER_ID/projects" 

不知道为什么请求:

curl --header "PRIVATE-TOKEN: PRIVATE_TOKEN" "https://gitlab.com/api/v4/projects/"
返回了包含其他一些公共项目的列表。

另一个有用的用户信息请求:

curl --header "PRIVATE-TOKEN: PRIVATE_TOKEN" "https://gitlab.com/api/v4/user/"


0
投票

https://docs.gitlab.com/ee/api/#basic-usage

尝试这个命令:curl“https://gitlab.example.com/api/v4/projects” 如文档中所示

如果您使用的是 Gitlab 版本 3 然后使用 curl --header“授权:承载 OAUTH-TOKEN” https://gitlab.example.com/api/v3/projects

还有“python-gitlab”模块,可以帮助您轻松从项目名称中获取 Id https://python-gitlab.readthedocs.io/en/stable/

© www.soinside.com 2019 - 2024. All rights reserved.