Can't find out base GitLab API base url

2019-06-15 02:44发布

问题:

I've created project and repo on my gitlab.com account, generated private key, now I'm trying to do api call to get list of commits.

Now I want to get list of projects via api, from documentation https://docs.gitlab.com/ce/api/projects.html#list-projects

GET /projects

So I'm doing

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

And getting 404. I've tried several combinations and can't find correct base url.

Same for repository commits, documentations https://docs.gitlab.com/ce/api/commits.html says

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

fine, I'm trying (with myusername/projectname as project id) https://gitlab.com/api/v3/projects/myusername/projectname/repository/commits

And got 404 as well

回答1:

The correct base url for the hosted GitLab is https://gitlab.com/api/v4/ so your request to
GET /projects would be

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

Also, the project ID is not the same as the project name. You can retrieve the project ID from the response of your request to GET /projects



回答2:

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

try this command : curl "https://gitlab.example.com/api/v4/projects" as given in the document

If you are using version3 of Gitlab then use curl --header "Authorization: Bearer OAUTH-TOKEN" https://gitlab.example.com/api/v3/projects

There is also "python-gitlab" module which will help you get Id from Project-name easily https://python-gitlab.readthedocs.io/en/stable/



回答3:

For me the following request worked:

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

Don't know why the request :curl --header "PRIVATE-TOKEN: PRIVATE_TOKEN" "https://gitlab.com/api/v4/projects/" returned a list with some other public projects.

Another useful request for user info: curl --header "PRIVATE-TOKEN: PRIVATE_TOKEN" "https://gitlab.com/api/v4/user/"



标签: api gitlab