Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何通过python中的请求访问github API?_Python_Github_Python Requests - Fatal编程技术网

如何通过python中的请求访问github API?

如何通过python中的请求访问github API?,python,github,python-requests,Python,Github,Python Requests,我试图通过python请求访问github API(回答类似问题,没有帮助) 使用curl,我可以获得一个项目最近提交的列表,例如 curl -H "Accept: application/vnd.github.inertia-preview+json Authorization: token a0d42faabef23ab5b5462394373fc133ca107890" https://api.github.com/repos/rsapkf/42/commit 尝试

我试图通过python请求访问github API(回答类似问题,没有帮助)

使用curl,我可以获得一个项目最近提交的列表,例如

curl -H "Accept: application/vnd.github.inertia-preview+json Authorization: token a0d42faabef23ab5b5462394373fc133ca107890" https://api.github.com/repos/rsapkf/42/commit 
尝试在python中对我尝试使用的请求使用相同的设置

url = "https://api.github.com/repos/rsapkf/42/commits"
headers = {"Accept": "application/vnd.github.inertia-preview+json", "Authorization": "token a0d42faabef23ab5b5462394373fc133ca107890"}
r = requests.get(url, headers=headers)
以及

url = "https://api.github.com/repos/rsapkf/42/commits"
headers = {"Accept": "application/vnd.github.inertia-preview+json"}
my_username = "someuser"
my_token  = "a0d42faabef23ab5b5462394373fc133ca107890"
r = requests.get(url, headers=headers, auth=(my_username, my_token))
但在这两种情况下,我都得到了回应

{'documentation_url': 'https://docs.github.com/rest',
 'message': 'Bad credentials'}

我在这里遗漏了什么?

不需要身份验证。以下工作:

url = "https://api.github.com/repos/rsapkf/42/commits"
headers = {"Accept": "application/vnd.github.inertia-preview+json"}
r = requests.get(url, headers=headers)

无耻的插件:我写了一个使github API的使用更容易的包:@Marat谢谢你的建议,但它似乎不起作用。我收到一个错误
requests.exceptions.HTTPError:401客户端错误:url未经授权…
您不能提供令牌,那么它就可以工作了!?!对于某些API,例如,要从您有权访问的私有存储库中读取,必须使用auth。此外,即使对于公共数据,未经验证的请求也仅限于每小时60次IPit每小时仅提供60次请求。原始代码中的问题是
my_username
应该是
token
而不是
someuser