gpt4 book ai didi

python - 如何使用 Python 使用 Github GraphQL API?

转载 作者:太空狗 更新时间:2023-10-29 20:13:25 25 4
gpt4 key购买 nike

我想使用 Github GraphQl v4 API 从 Github 访问详细信息。我找到了 Graphene库,但我不确定如何在 Python 中使用个人访问 token 进行身份验证。
我试图在谷歌上搜索但找不到任何例子。它是 Python 库,可以创建图形模式而不是用于使用它们,我尝试使用“请求”但失败了。我如何进行身份验证并找到存储库列表?

我已经使用 Github GraphQl 资源管理器通过以下代码查找存储库列表:

viewer {
repositories(first: 30) {
totalCount
pageInfo {
hasNextPage
endCursor
}
edges {
node {
name
}
}
}

最佳答案

与 rest 不同,graphql 只有一个端点。您只需将查询作为 json 对象执行 POST。您应该提供从 github 获得的 api_token作为 header 的一部分。

import requests

url = 'https://api.github.com/graphql'
json = { 'query' : '{ viewer { repositories(first: 30) { totalCount pageInfo { hasNextPage endCursor } edges { node { name } } } } }' }
api_token = "your api token here..."
headers = {'Authorization': 'token %s' % api_token}

r = requests.post(url=url, json=json, headers=headers)
print (r.text)

关于python - 如何使用 Python 使用 Github GraphQL API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45957784/

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