【发布时间】:2020-07-17 01:45:49
【问题描述】:
我正在尝试使用 python 程序从 github 检索 xml 文件。我正在尝试不同的网址,但对于每个网址,它都说找不到内容。从 github 检索文件有哪些不同的选项?对此的任何帮助将不胜感激!
import requests
from github import Github
import base64
g = Github("access_token")
#url = 'https://api.github.com/repos/{username}/{repos_name}/contents/{path}/{filename}.xml'
#url = 'https://git.<company domain>.com/raw/IT/{repos_name}/{path}/{filename}.xml?token<value>'
url = 'https://git.<company domain>.com/raw/IT/{repos_name}/{path}/{filename}.xml?token=<value>'
req = requests.get(url)
#print ('Keep Going!', req.content)
if req.status_code == requests.codes.ok:
req = req.json()
`. `# the response is a JSON
# req is now a dict with keys: name, encoding, url, size ...
# and content. But it is encoded with base64.
content = base64.decodestring(req['content'])
else:
print('Content was not found.')
输出:
Keep Going! b'{"message":"Not Found","documentation_url":"https://developer.github.com/v3/repos/contents/#get-contents"}'
Content was not found.
【问题讨论】:
-
根据输出,似乎目的地可能存在问题。你确定不是这样吗?
-
repos_name,path,filename, ... 永远不会格式化,也不会在代码中的任何位置声明。 -
这就是我想要弄清楚的。从 github 获取正确 url 的最佳方法是什么?我试图从“原始”选择中输入普通网址,但它们都不起作用。
-
@andrea - 为了保密,我已将实际名称替换为占位符。在运行代码时,我将 repos_name、路径、文件名与实际值一起传递
-
如果没有看到完整的 URL,很难判断问题出在哪里。我建议尝试使用公共回购,例如你能用你的代码来检索this file吗?另外,我不是 GitHub 专家,但在我看来 authentication is only supported via headers--您将令牌作为 URL 参数传递,我认为这不受支持
标签: python github github-api