首页 > 解决方案 > 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

标签: node.jsgithub-apinode-requestprobot

解决方案


404 的可能原因:

  • 存储库是私有的,您无权访问(这需要标头"Authorization: token $TOKEN"
  • JSON 响应(因为默认答案是 JSON 文件内容以 base64 编码)超过 1MB。Get Contents API确实提到“此 API 支持最大 1 兆字节的文件” 。
    使用标题“ Accept: application/vnd.github.3.raw”将为您提供原始内容。

推荐阅读