Can't find out base GitLab API base url - api

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

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"
That would return all projects that are visible to you, including other user's public projects.
If you wish to view just your projects, then you should use the 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 if you're authenticated.
# 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"
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 /users/:user_id/projects, or from the project's settings page.

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/"

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/

Related

Not able to create ENV variable using API token

I was trying to automate creating ENV variables in CI/CD by using API commands, but unfortunately getting 401 Unauthorized error.
Earlier I used to do this in same way and was able to do it.
But now it’s throwing an error, could anyone please help me to find out.
Command:
curl --request POST --header “PRIVATE-TOKEN: <your_access_token>” \
“https://gitlab.com/api/v4/projects/1/variables” --form “key=NEW_VARIABLE” --form “value=new value”
Please look into the error message:
The "Create a variable" API call indeed looks like:
curl --request POST --header "PRIVATE-TOKEN: <your_access_token>" \
"https://gitlab.com/api/v4/projects/1/variables" --form "key=NEW_VARIABLE" --form "value=new value"
(make sure to use the right double-quotes " instead of “)
Double-check the ID of the project (in your case "1") and make sure the user authenticated with your token has the right permissions:
They must be "maintainer" or "owner", in order to have the right to "Manage project-level CI/CD variables".
Example, using a Personal Access Token with api scope, starting with glpat- (glpat-xxxxxxx), I can first list my projects (using jq):
curl -XGET --header "PRIVATE-TOKEN: glpat-xxxx" "https://gitlab.com/api/v4/projects/owned?=true"|jq ".[] | \"\(.id) \(.path_with_namespace)\""
That allows me to find the project id
I can then list variables for an existing project:
curl -XGET --header "PRIVATE-TOKEN: glpat-xxxx" "https://gitlab.com/api/v4/projects/<projectId>/variables"
Result:
[]
I have none on that project.
I will set one with:
curl -XPOST --header "PRIVATE-TOKEN: glpat-xxxx" "https://gitlab.com/api/v4/projects/<projectId>/variables" --form "key=NEW_VARIABLE" --form "value=new value"
Result:
{"variable_type":"env_var","key":"NEW_VARIABLE","value":"new value","protected":false,"masked":false,"environment_scope":"*"}
Let's double-check with:
curl -XGET --header "PRIVATE-TOKEN: glpat-xxxx" "https://gitlab.com/api/v4/projects/<projectId>/variables"
Result:
[{"variable_type":"env_var","key":"NEW_VARIABLE","value":"new value","protected":false,"masked":false,"environment_scope":"*"}]
It does work.
The OP Anirban Das confirms in the comments an issue with how Postman was used:
Actually in the body of Postman, select 'form', there I had mentioned directly key name in the Key section and value in the Value section.
But that was not correct.
In the Key section, we need to mention "key" and key name should be in Value section.
Similarly "value" in key section and it's value in Value section.
Once this worked, you will see "</>" icon in right navigation pane, which will provide you corresponding curl command

Hitting the GitHub api for listing PR comments returns no result

I want to list (count actually) the comments made on a GitHub pull request.
As per the documentation, I am using the following curl command to hit the GitHub API
curl -H "Accept: application/vnd.github+json" -H "Authorization: token abcdefg123457" https://api.github.com/repos/MyOrg/MyRepo/pulls/2/comments
The result is the following:
[
]
despite me seeing comments on #PR2.
What am I missing?

How to create new resource from a file via CKAN API with cURL?

The following cURL is from: https://tech.datopian.com/ckan/play-around.html#create-organizations-datasets-and-resources
and lets you add a resource to a datset from a url.
curl -X POST http://ckan:5000/api/3/action/resource_create -H "Authorization: 9c04a69d-79f4-4b4b-b4e1-f2ac31ed961c" -d '{
"package_id": "my-first-dataset",
"url": "https://raw.githubusercontent.com/frictionlessdata/test-data/master/files/csv/100kb.csv",
"description": "This is the best resource ever!" ,
"name": "brand-new-resource"
}'
Was wondering how you could add a resource from a file.
I can share my example request from a Postman collection (hope it helps):
curl --location --request POST 'http://local-ckan/api/3/action/resource_create' \
--header 'Authorization: xxx-yyy-zzz' \
--form 'package_id="dataset-name"' \
--form 'upload=#"/home/tomek/Downloads/example.csv"' \
--form 'name="new-resource-with-file"'
And here's a note on creating resources from my achives while I was dealing with uploading files to CKAN:
API calls are either POST with a JSON body or POST with a multipart/form body. When using multipart/form you can't nest anything (e.g. resources) in the same call so you have to make multiple calls (package_create, resource_create, resource_create, ...). When using JSON you can't upload files as part of the same call so you need to attach any files with separate multipart/form calls e.g. with resource_patch

Getting "You don't have a required scope to access the endpoint" when inserting script

I'm developing an app, for store fronts and want to get some analytics in checkout. So I want to inject a script in that scope of checkout. When I try to insert it I'm getting "You don't have a required scope to access the endpoint" but I have updated the scopes to checkoutcontent to modify. Not sure what else is wrong
Trying to insert script via an app, getting 403 even though I updated the OAuth scopes to include, Check out content and Checkout
curl --request POST \
--url https://api.bigcommerce.com/stores/{store_hash}/v3/content/scripts \
--header 'accept: application/json' \
--header 'content-type: application/json' \
--header 'x-auth-client: XXXXX' \
--header 'x-auth-token: XXXXX' \
--data '{"name":"Test Scripts Tag","description":"Test Scripts Tag","html":"<script src=\\\"https://Somedestination/Test.js\\\"></script>","src":"https://Somedestination/Test.js","auto_uninstall":true,"load_method":"default","location":"footer","visibility":"checkout","kind":"src"}'
Getting below error, while expecting a status=200
status: 403,You don't have a required scope to access the endpoint
The html field shouldn't be included when using src, could you try removing it?
The only errors I was receiving in testing were due to malformed HTML in the html field with the error code 422. It may also be worth trying to create a new API account to rule out scoping causing this.

How to update gitlab merge request with curl? Getting 401 all the time

Need to update labels in merge request during gitlab ci. Tried different variants:
curl -X PUT https://gitlab.com/api/v4/projects/{project_id}/merge_requests/134?access_token={token}&labels=merged
Tried passing token as header and as data.
Tried passing "personal_token", "access_token", "token" and etc.
Getting {"message":"401 Unauthorized"} all the time.
I think that you should use private_token as it said on documentation:
curl https://gitlab.example.com/api/v4/projects?private_token=<your_access_token>
or in the header, as you said:
curl --header "PRIVATE-TOKEN: xxx" "https://gitlab.com/api/v4/projects"
Have a look on the official page:
https://docs.gitlab.com/ee/api/README.html#personal-access-tokens
Example:
Getting information from merge request with iid 1(the actual labels are testlabel and testlabel3):
#curl -X GET https://gitlab.com/api/v4/projects/11209705/merge_requests/1?private_token={XXXXXX}
{"id":28761206,"iid":1,"project_id":1120970.......,"labels":["testlabel","testlabel3"],...}
Sending put with curl with "--data" and the parameter to update "labels" to "test":
# curl -X PUT https://gitlab.com/api/v4/projects/11209705/merge_requests/1?private_token={XXXXX} --data "labels=test"
Getting information from merge request with iid 1(the actual labels is "test"):
#curl -X GET https://gitlab.com/api/v4/projects/11209705/merge_requests/1?private_token={XXXXXX}
{"id":28761206,"iid":1,"project_id":1120970.......,"labels":["test"],...}