繁体   English   中英

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

[英]How to access the github API via requests in python?

我正在尝试通过使用 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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM