Yen*_*the 6 github-api python-3.x python-requests
我正在查看 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))
Run Code Online (Sandbox Code Playgroud)
然后我可以url像这样取出每个请求:
for repository_invite in repository_invites:
print('url: ' + repository_invite.get('url'))
Run Code Online (Sandbox Code Playgroud)
这给出了一些东西:
url: https://api.github.com/user/repository_invitations/123456789
Run Code Online (Sandbox Code Playgroud)
Github 还提到你可以在https://developer.github.com/v3/repos/invitations/#accept-a-repository-invitation接受邀请,其中提到
PATCH /user/repository_invitations/:invitation_id
我不明白的是我如何告诉 Github 如何接受它。此端点似乎用于删除和接受邀请。GithubPATCH在https://developer.github.com/v3/#http-verbs上谈到,其中提到您可以使用 aPOST或发送PATCH请求,但不是如何使用。所以问题是,我怎么知道我应该在PATCH电话中发送什么?我试过这个例如:
result = requests.patch(repository_invite.get('url'), json.dumps({'accept': True}))
print('result: ' + str(result.json()))
Run Code Online (Sandbox Code Playgroud)
哪个回馈:
result: {'message': 'Invalid request.\n\n"accept" is not a permitted key.', 'documentation_url': 'https://developer.github.com/v3'}
Run Code Online (Sandbox Code Playgroud)
为了调用 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))
Run Code Online (Sandbox Code Playgroud)
如果没有身份验证,Patch调用将返回 404 响应代码,因为出于明显的安全目的,只能在登录后访问它。如果您调用端点user/repository_invitations/并添加 ID,Github 将自动接受邀请。
| 归档时间: |
|
| 查看次数: |
1374 次 |
| 最近记录: |