GitHub API 是 GitHub 提供的 RESTful API,允许开发者以编程方式与 GitHub 交互,包括查询、创建、修改存储库、问题、拉取请求等资源。
GitHub 提供两种主要 API:
import requests
# 设置你的 GitHub 个人访问令牌
TOKEN = "your_personal_access_token"
ORG_NAME = "your_organization_name"
headers = {
"Authorization": f"token {TOKEN}",
"Accept": "application/vnd.github.v3+json"
}
# 获取组织存储库
url = f"https://api.github.com/orgs/{ORG_NAME}/repos"
response = requests.get(url, headers=headers)
if response.status_code == 200:
repos = response.json()
for repo in repos:
print(f"Repository: {repo['name']}")
print(f"Description: {repo['description']}")
print(f"URL: {repo['html_url']}")
print("---")
else:
print(f"Error: {response.status_code}")
print(response.json())
import requests
TOKEN = "your_personal_access_token"
ORG_NAME = "your_organization_name"
headers = {
"Authorization": f"bearer {TOKEN}",
"Content-Type": "application/json"
}
query = """
query($org: String!, $cursor: String) {
organization(login: $org) {
repositories(first: 100, after: $cursor) {
edges {
node {
name
description
url
}
}
pageInfo {
endCursor
hasNextPage
}
}
}
}
"""
def get_repos():
repos = []
cursor = None
has_next_page = True
while has_next_page:
variables = {
"org": ORG_NAME,
"cursor": cursor
}
response = requests.post(
"https://api.github.com/graphql",
headers=headers,
json={"query": query, "variables": variables}
)
if response.status_code == 200:
data = response.json()
org_data = data["data"]["organization"]
repos.extend([edge["node"] for edge in org_data["repositories"]["edges"]])
page_info = org_data["repositories"]["pageInfo"]
has_next_page = page_info["hasNextPage"]
cursor = page_info["endCursor"]
else:
print(f"Error: {response.status_code}")
print(response.json())
break
return repos
repositories = get_repos()
for repo in repositories:
print(f"Repository: {repo['name']}")
print(f"Description: {repo['description']}")
print(f"URL: {repo['url']}")
print("---")
原因:未提供有效令牌或令牌权限不足 解决:
repo
或 read:org
权限原因:GitHub API 有严格的速率限制 解决:
原因:组织存储库数量超过单页限制(默认30) 解决:
per_page
参数增加每页数量(最大100)原因:令牌权限不足或组织设置限制 解决:
repo
权限没有搜到相关的文章