Logo
Section 1 / 3

协作者

学习并实践 GitHub API 中协作者的功能。

导入

协作者

学习并实践 GitHub API 中协作者的功能。

概述

协作者 是由仓库所有者邀请参与软件开发的用户。读写权限意味着协作者可以拉取仓库内容,进行所需修改,并将更改推回仓库。仓库所有者会向希望合作的用户发送协作邀请,该邀请需在七天内接受,逾期将失效。一个仓库可添加的协作者数量没有限制。

...

获取仓库的协作者列表

具有推送权限的已认证用户可以使用此接口。可通过提供访问令牌进行认证。返回的协作者列表包含所有拥有仓库默认权限的成员。

以下代码用于获取仓库的协作者列表:

1headers = { 2 'Authorization': 'token {{ACCESS_TOKEN}}', 3 'Accept': 'application/vnd.github.v3+json' 4} 5 6url = 'https://api.github.com/repos/{{USERNAME}}/{{REPOSITORY}}/collaborators' 7 8response = requests.get(url, headers=headers) 9print(json.dumps(response.json(), indent=4))
1headers = { 2 'Authorization': 'token {{ACCESS_TOKEN}}', 3 'Accept': 'application/vnd.github.v3+json' 4} 5 6url = 'https://api.github.com/repos/{{USERNAME}}/{{REPOSITORY}}/collaborators/<username>' 7 8response = requests.get(url, headers=headers) 9 10if response.status_code == 204: 11 print('该用户是协作者') 12else: 13 print('该用户不是协作者')
1headers = { 2 'Authorization': 'token {{ACCESS_TOKEN}}', 3 'Accept': 'application/vnd.github.v3+json' 4} 5 6url = 'https://api.github.com/repos/{{USERNAME}}/{{REPOSITORY}}/collaborators/<username>' 7 8data = json.dumps({ 9 'permission': '<permission>' 10}) 11 12response = requests.put(url, headers=headers, data=data) 13print(json.dumps(response.json(), indent=4))
1headers = { 2 'Authorization': 'token {{ACCESS_TOKEN}}', 3 'Accept': 'application/vnd.github.v3+json' 4} 5 6url = 'https://api.github.com/repos/{{USERNAME}}/{{REPOSITORY}}/collaborators/<username>' 7 8response = requests.delete(url, headers=headers) 9 10print(response)
1headers = { 2 'Authorization': 'token {{ACCESS_TOKEN}}', 3 'Accept': 'application/vnd.github.v3+json' 4} 5 6url = 'https://api.github.com/repos/{{USERNAME}}/{{REPOSITORY}}/collaborators/<username>/permission' 7 8response = requests.get(url, headers=headers) 9print(json.dumps(response.json(), indent=4))

代码管理

拉取请求