node.js - GitHub API - "Get contents" continually returning 404 for valid path
问题描述
I'm using probot => https://probot.github.io/
I've been developing a GitHub application that analyses a specific .json
file in a repo for changes to date strings.
I do this by subscribing to the push
event and watching it with a webhook
.
I am using request
in Node. The issue I am having is that I continually receive a 404
when the hook runs. My code looks like this:
app.on('push', async context => {
let repoOwner = context.payload.repository.owner.name;
let repoName = context.payload.repository.name;
const options = {
url: `https://api.github.com/repos/${repoOwner}/${repoName}/contents/file.json`,
headers: { 'User-Agent': 'request' }
}
request.get(options, (error, response, body) => {
console.log(body) // logs {message: 'Not Found', documentation_url:... etc
})
})
previously I was not including a user-agent
header which was constantly returning a 403
- GitHub's api specifies that you must pass a header. After doing this I am now constantly getting this 404
解决方案
404 的可能原因:
- 存储库是私有的,您无权访问(这需要标头
"Authorization: token $TOKEN"
) - JSON 响应(因为默认答案是 JSON 文件内容以 base64 编码)超过 1MB。Get Contents API确实提到“此 API 支持最大 1 兆字节的文件” 。
使用标题“Accept: application/vnd.github.3.raw
”将为您提供原始内容。
推荐阅读
- python - 如果函数在不应该执行时执行
- c# - 在访问者模式中的节点之间传递数据
- javascript - 在我的代码中,mousemove 属性不起作用并更改了输入类型范围。能解释一下为什么吗?
- python - 使用 python 中的命令打开多个 selenium webdrivers(任务)
- python - VSCode忽略断点
- django - 如何通过模板从数据库中删除记录?
- python - 使用 selenium.webdriver 登录后,如何识别包含指向我的 linkedin 配置文件的链接的元素?
- azure - Azure OAuth:无法在管理员同意的情况下以编程方式创建应用以获得权限
- c# - 从不同类型的列表中搜索名称
- yii2 - Yii2模型规则黑名单词数组检查