gpt4 book ai didi

python-3.x - 如何通过 API 接受 Github 仓库邀请?

转载 作者:行者123 更新时间:2023-12-03 16:35:28 24 4
gpt4 key购买 nike

我正在查看 Github API,它允许您通过 API 端点获取所有存储库邀请(请参阅 https://developer.github.com/v3/repos/invitations/#list-invitations-for-a-repository )。这可以正常工作:

from requests.auth import HTTPBasicAuth
import requests
login = 'xxx'
password = 'yyy'
url = 'https://api.github.com/user/repository_invitations'
repository_invites = requests.get(
url, auth=HTTPBasicAuth(login, password)).json()
print('response: ' + str(repository_invites))


然后我可以像这样取出每个请求的 url:
for repository_invite in repository_invites:
print('url: ' + repository_invite.get('url'))

这给出了一些类似的东西:
url: https://api.github.com/user/repository_invitations/123456789

Github 还提到你可以在 https://developer.github.com/v3/repos/invitations/#accept-a-repository-invitation 接受邀请,其中提到

PATCH /user/repository_invitations/:invitation_id



我不明白的是我如何告诉 Github 如何接受它。此端点似乎用于删除和接受邀请。 Github 在 https://developer.github.com/v3/#http-verbs 上讨论了 PATCH,其中提到您可以使用 POST 或发送 PATCH 请求,但没有说明如何使用。所以问题是,我怎么知道我应该在 PATCH 调用中发送什么?我试过这个例如:
result = requests.patch(repository_invite.get('url'), json.dumps({'accept': True}))
print('result: ' + str(result.json()))

哪个回馈:
result: {'message': 'Invalid request.\n\n"accept" is not a permitted key.', 'documentation_url': 'https://developer.github.com/v3'}

最佳答案

为了调用 API 端点,您需要对您的 Github 用户进行身份验证,并且您需要发送一个 Patch调用(如果需要,可以获取数据/标题)。这是一个工作示例:

for repository_invite in repository_invites:
repository_id = repository_invite.get('id')
accept_invite = requests.patch('https://api.github.com/user/repository_invitations/'+ str(repository_id),
data={}, headers={},
auth=HTTPBasicAuth(github_username, github_password))

没有认证的 Patch调用将返回 404 响应代码,因为出于明显的安全目的,它只能在登录后访问。如果您调用端点 user/repository_invitations/后跟 ID Github 会自动接受邀请。

关于python-3.x - 如何通过 API 接受 Github 仓库邀请?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61276445/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com