找不到 base GitLab API base url
Can't find out base GitLab API base url
我已经在我的 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。
我尝试了几种组合,但找不到正确的基数 url.
存储库提交相同,文档 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
托管的 GitLab 的正确基础 url 是 https://gitlab.com/api/v4/
所以你的请求
GET /projects
会是
curl --header "PRIVATE-TOKEN: XXXXXX" "https://gitlab.com/api/v4/projects"
这将 return 您可见的所有项目,包括其他用户的 public 项目。
如果您只想查看您的项目,那么您应该使用 GET /users/:user_id/projects
endpoint, where :user_id
is your user ID that can be found on your GitLab profile page or in the response to your request to 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。
https://docs.gitlab.com/ee/api/#basic-usage
试试这个命令:curl "https://gitlab.example.com/api/v4/projects"
如文档中所述
如果你使用的是版本3的Gitlab
然后使用
curl --header "Authorization: Bearer OAUTH-TOKEN" https://gitlab.example.com/api/v3/projects
还有 "python-gitlab" 模块可以帮助您轻松地从 Project-name 获取 Id
https://python-gitlab.readthedocs.io/en/stable/
对我来说,以下请求有效:
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/"
返回了一个包含其他 public 个项目的列表。
另一个有用的用户信息请求:curl --header "PRIVATE-TOKEN: PRIVATE_TOKEN" "https://gitlab.com/api/v4/user/"
我已经在我的 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。 我尝试了几种组合,但找不到正确的基数 url.
存储库提交相同,文档 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
托管的 GitLab 的正确基础 url 是 https://gitlab.com/api/v4/
所以你的请求
GET /projects
会是
curl --header "PRIVATE-TOKEN: XXXXXX" "https://gitlab.com/api/v4/projects"
这将 return 您可见的所有项目,包括其他用户的 public 项目。
如果您只想查看您的项目,那么您应该使用 GET /users/:user_id/projects
endpoint, where :user_id
is your user ID that can be found on your GitLab profile page or in the response to your request to 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。
https://docs.gitlab.com/ee/api/#basic-usage
试试这个命令:curl "https://gitlab.example.com/api/v4/projects" 如文档中所述
如果你使用的是版本3的Gitlab 然后使用 curl --header "Authorization: Bearer OAUTH-TOKEN" https://gitlab.example.com/api/v3/projects
还有 "python-gitlab" 模块可以帮助您轻松地从 Project-name 获取 Id https://python-gitlab.readthedocs.io/en/stable/
对我来说,以下请求有效:
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/"
返回了一个包含其他 public 个项目的列表。
另一个有用的用户信息请求:curl --header "PRIVATE-TOKEN: PRIVATE_TOKEN" "https://gitlab.com/api/v4/user/"