Create new gist with Github API v3 using curl

2019-03-27 20:04发布

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.

1条回答
ゆ 、 Hurt°
2楼-- · 2019-03-27 20:58

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.

查看更多
登录 后发表回答