Github-api通过python + urllib2传递json-data时给出404 [英] Github-api giving 404 when passing json-data with python + urllib2

查看:149
本文介绍了Github-api通过python + urllib2传递json-data时给出404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,该代码应执行在github上创建新下载的第一部分.它应该使用POST发送json-data.

I have the following code, which should perform the first part of creating a new download at github. It should send the json-data with POST.

jsonstring = '{"name": "test", "size": "4"}'
req = urllib2.Request("https://api.github.com/repos/<user>/<repo>/downloads")
req.add_header('Authorization', 'token ' + '<token>')
result = urllib2.urlopen(req, jsonstring)

如果我从urlopen()中删除, jsonstring,它不会失败,并提供可用下载的列表.但是,如果我尝试发布json-string,则会收到404错误.

If I remove the , jsonstring from the urlopen(), it does not fail, and gives me the list of available downloads. However, if I try to POST the json-string, I get 404 error.

问题必须出在json上,或者是以我发送的方式出现,但我无法弄清楚问题出在哪里. <...>处的字符串在实际代码中正确,我刚刚从帖子中删除了它们

The problem has to be with the json, or in the way I send it, but I can't figure out what the problem is. The strings at <...> are right in the actual code, I just removed them from the post

我在命令行上使用curl尝试了大致相同的方法,但认证方式略有不同,并且可以正常工作.

I tried roughly the same with curl on the command-line, with slightly different method of authentication, and it worked.

已测试:

Works(返回所需的json):

Works(returns the wanted json):

curl -u "user:password" --data "json..." https://api.github.com/repos/<user>/<repo>/downloads

作品:

curl -H 'Authorization: token <token>' https://api.github.com/

不起作用(返回无效的凭据"):

Does not work (returns "invalid credentials"):

curl -H 'Authorization: token <invalid_token>' https://api.github.com/

不起作用(未找到"):

Does not work ("not found"):

curl -H 'Authorization: token <valid_token>' --data "json..." https://api.github.com/repos/<user>/<repo>/downloads

这似乎不是特定于python代码的问题. json POST数据似乎很好,并且OAuth令牌授权似乎(至少部分地)正常工作.但是,将它们放在一起后,它将停止工作.

This does not seem to be an issue specific to the python code. The json POST data seems to be fine, and the OAuth token authorization seems to be (atleast partly) working. But when these are put together, it stops working.

推荐答案

我终于可以解决了,为什么它不起作用.

I was finally able to work out, why it did not work.

我没有正确设置授权令牌的授权范围.我使用的令牌没有被授权"做任何修改,并且使用它的所有尝试修改某项操作(添加下载)的操作都失败了.

I did not have the autorization scopes for the authorization token set correctly. The token I was using, was not "authorized" to do any modifications, and every action using it, that tried to modify something (add a download), failed.

我必须在授权中添加正确的作用域才能使其正常工作.

I had to add the correct scopes to the authorization to make it work.

这篇关于Github-api通过python + urllib2传递json-data时给出404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆