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

2024-09-29 17:11:06 发布

您现在位置:Python中文网/ 问答频道 /正文

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

使用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'}

我错过了什么


Tags: httpsgithubcomtokenapijsonurlapplication
1条回答
网友
1楼 · 发布于 2024-09-29 17:11:06

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

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

相关问题 更多 >

    热门问题