Create new gist with Github API v3 using curl - json

After fighting for quite some time for posting a private gist to Github using their API V3 I almost gave up. Almost. May be some one have also faced similar problem or know what might be the reasoning of the following behavior:
Right now the curl command looks like following:
curl -H "Authorization: bearer MY_AUTHORIZATION_TOKE" -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"public":false,"files":{"test.txt":{"content":"String file contents"}}}' https://api.github.com/gists
I also tried
curl -H "Authorization: bearer MY_AUTHORIZATION_TOKE" -X POST -d '{"public":false,"files":{"test.txt":{"content":"String file contents"}}}' https://api.github.com/gists
I am able to create gist without authorization token using exactly same data:
curl -X POST -d '{"public":true,"files":{"test.txt":{"content":"String file contents"}}}' https://api.github.com/gists
But in that case it will be anonymous
Same results if I am truing to post it as public
In any case Github returns me
HTTP/1.1 404 Not Found
{
"message": "Not Found"
}
I am pretty sure I am authorized, as curl -H "Authorization: bearer MY_AUTHORIZATION_TOKE" https://api.github.com/user returns me my user details.
Application scope is as:
https://github.com/login/oauth/authorize?client_id=...&scope=gist
So, it should have both read and write permission.

Your OAuth2 token doesn't appear to have the required gist scope.
If you run the curl commands with the -v argument you can see the scope sent to request (X-OAuth-Scopes header) and the scope required for the request (X-Accepted-OAuth-Scopes header) to successfully be performed using the token sent.
If you don't see gist listed in the X-OAuth-Scopes header value then that is your problem.

Related

How can I fix this cURL PUT request to the Canvas API to bulk update course settings

In this cURL request I am attempting to do a bulk update to course settings to change the visibility to public. I'm a canvas admin and have the correct access token/url in my working script. The response I receive is "must specify course_ids[]" I have double checked to make sure I'm using accurate course ids. The canvas API documentation has been helpful but I cannot seem to correct it.
curl https://myschool.instructure.com/api/v1/accounts/1/courses -X PUT
-H "Authorization: Bearer token"
-H "Content-Type: application/json"
-d '{
"course_ids[]": [7504, 7505, 7506,...],
"course": {
"is_public": true
}
}'

User not authenticated

Im trying to download a file as described in forge documentation.
So im getting an access token with scope=data:read for the first 2 steps :
curl
-v 'https://developer.api.autodesk.com/authentication/v1/authenticate'
-X 'POST'
-H 'Content-Type: application/x-www-form-urlencoded'
-d 'client_id=xxx&client_secret=xxx&grant_type=client_credentials&scope=data:read'
It works up to the GET hubs/:hub_id/projects endpoint.
Then, the GET projects/:project_id/folders/:folder_id/contents endpoint requires data:write, so im adding it:
curl
-v 'https://developer.api.autodesk.com/authentication/v1/authenticate'
-X 'POST'
-H 'Content-Type: application/x-www-form-urlencoded'
-d 'client_id=xxx&client_secret=xxx&grant_type=client_credentials&scope=data:read%20data:write'
It returns an access token without any issue, but when i use for this endpoint, i get a "User not authenticated" error.
I honestly dont know where its coming from, i tried without the url-encoded space as well, no difference.
Thanks in advance for any pointers in the right direction.

Passing X-Authentication via CURL on command line

I'm trying to curl a URL using GET which requires authorisation but no matter how I pass the token, I'm getting this returned:
{
"response": {
"error": "Authentication required",
"error_code": 1
}
}
I've asked the developer of the API I'm using but all I've been told is "The token must be included in the X-Authorization HTTP header" which doesn't tell me what I'm doing wrong.
Here's an example of one of the commands I've tried:
curl -X GET -H 'Content-Type: application/json' -H "Authorization: Bearer MyTokenGoesHere" -k https://example.com/SomeFolder/endofAPIURL?ArgumentOne=foo&ArgumentTwo=bar
I've tried multiple ways to pass the token but I'm clearly doing something wrong because none of them seem to be even passing the token as I'm getting "Authentication required" instead of incorrect/invalid.
Here are all the ways I've tried passing the token:
-H "Authorization: MyTokenGoesHere"
-H "Authorization: Basic MyTokenGoesHere"
-H "Authorization: Bearer MyTokenGoesHere"
-H "Authorization: Oauth MyTokenGoesHere"
-H "X-Auth-Token: MyTokenGoesHere"
-H "X-Auth-Token: Basic MyTokenGoesHere"
-H "X-Auth-Token: Bearer MyTokenGoesHere"
-H "X-Auth-Token: Oauth MyTokenGoesHere"
All of these different ways of passing the token have been taken from various posts on this site.
I feel I'm missing something obvious but I just can't see it.
So it turns out that the API was using a custom header but didn't have it documented anywhere. The documentation provided also used X-Auth-Token (which I believe is custom in itself but could be mistake) which is why I was trying that.
In my particular case, what I needed was -H "AuthTo: MyTokenGoesHere"

How to call REST API using CURL & SAML TOKEN Auth

My Url is Like:
https://<ip:port>/TestRESTServices/objects/test-folder
JSON data that I want to pass is:
{
"name":"test-1",
"parent-uuid":"126"
}
test-1 is the folder name which i want to create.
When i invoke this url with the data in Poster plugin in firefox via POST it works fine and folder test-1 is created.
//using Content Type : "application/json"
How can I invoke/call this REST API using cURL ?
Need Help.
This is what i tried:
curl -i -H "Accept: application/json" -X POST -d '{"name":"test-1","parent-uuid":"126"}' https://<ip:port>/TestRESTServices/objects/test-folder
It throws an error that curl: (52) Empty reply from server
Unfortunately I don't have a REST API online to try it, but resources that I found suggest the following approaches:
curl -v -H "Content-Type: application/json" -X POST --data "#issue.json" -u login:password http://redmine/issues.json
where the issues.json is a file containing the JSON request.
Resources I found useful:
1, 2
Hope it helps!
For Authentication : Give the userid/password as admin:password
TOKEN=$(curl -s -k -X POST --basic -u "admin:password" "{host}/TestAuthServices/auth/tokens" | sed -rn 's/\{"Token":"([^"]+)".+/\1/p')
After getting this token call curl as:
curl -s -k -X POST -H "Content-Type: application/json" -H "Authorization: X-SAML ${TOKEN}" -d '{"name":"test","parent-uuid":"126"}' "{host}/TestRESTServices/objects/test-folder"

Github returns "Problems parsing JSON" when trying to create repo using curl

I'm attempting to create a repo on github using curl. No matter how I attempt to format my JSON it always returns "Problems parsing JSON". Is there something I'm missing? Here is my statement:
curl --data '{"name":"newRepoName"}' -XPOST -u username:password http://giturl
Strange since it works for me. Are you using the correct GitHub API endpoint (/user/repos)?
curl --data '{"name":"testrepo"}' -X POST -u username https://api.github.com/user/repos
Try including your token or password like this
curl --data '{"name":"testrepo"}' \
-X POST -u username:$TOKEN \
https://api.github.com/user/repos