首页 > 解决方案 > GitHub API v3:创建问题时出现 404 错误

问题描述

我正在关注GitHub Docs尝试获取问题列表并发布问题。

我已经设法使用

GET https://api.github.com/repos/wheatup/wheatup.github.io/issues

但是当我尝试将问题发布到 repo 时,我收到了以下正文的 404 错误:

{
  "message": "Not Found",
  "documentation_url": "https://docs.github.com/rest/reference/issues#create-an-issue"
}

这是我的发帖请求:

网址

POST https://api.github.com/repos/wheatup/wheatup.github.io/issues

标头

Accept: application/vnd.github.v3+json
Accept-Encoding: gzip, deflate, br
Accept-Language: en,zh-CN;q=0.9,zh;q=0.8,ja;q=0.7,zh-TW;q=0.6,sr;q=0.5,pl;q=0.4,la;q=0.3
Authorization: token d7fa1e545c*******************31957a97e06
Cache-Control: no-cache
Connection: keep-alive
Content-Length: 79
Content-Type: application/json
DNT: 1
Host: api.github.com
Origin: http://localhost:3000
Pragma: no-cache
Referer: http://localhost:3000/
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: cors
Sec-Fetch-Site: cross-site
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36

身体

{ 
  title: "Test", 
  body: "test content", 
  labels: [], 
  assignees: [], 
  milestone: 1
}

这就是我发布请求的方式:

const result = await axios.post('https://api.github.com/repos/wheatup/wheatup.github.io/issues', {
    title: 'Test',
    body: 'test content',
    labels: [],
    assignees: [],
    milestone: 1,
  }, {
    headers: {
      'Authorization': 'token d7fa1e545c*******************31957a97e06',
      'Content-Type': 'application/json',
      'Accept': 'application/vnd.github.v3+json'
    }
});

我的回购是公开的,我错过了什么吗?任何帮助将不胜感激!

标签: javascriptgithubpostgithub-api

解决方案


看来您在请求中缺少 github 令牌。在我添加不记名令牌之前,我在本地得到 404。然后我得到 401,因为我没有使用实际的不记名令牌来访问您的回购。因此,一旦您添加了该部分,一切都应该有效。

解决方案1:

    const result = await axios.post('https://api.github.com/repos/wheatup/wheatup.github.io/issues', {
            title: 'Test',
            body: 'test content',
            // labels: [], --> Since empty, commented out as it is optional param
            // assignee: '', --> Since empty, commented out as it is optional param. Also you had a typo and this attributes expects string not array
            milestone: 1,
        }, {
        headers: {
           'Authorization': `Bearer ${githubToken}`,
           'Content-Type': 'application/json',
           'Accept': 'application/vnd.github.v3+json'
        }
    });

在处理 github API 时,我建议使用他们的工具包,因为您只需要提供一次令牌,然后后续请求就可以将数据提供给他们

替代解决方案 2: --> 这仅适用于不必处理在每个请求上传递承载令牌,因此如果您宁愿继续使用 axios,请忽略。

  const octokit = new Octokit({ auth: githubToken });

  const response = await octokit.request('POST /repos/{owner}/{repo}/issues', {
       owner: 'wheatup',
       repo: 'wheatup.github.io',
       title: 'Test',
       body: 'test content',
       milestone: 1,
  });

编辑

解决方案3: ->上面提供的问题的实际解决方案

在处理 OAuth 应用程序时,需要采取一些步骤

  1. 用户被重定向以请求他们的 GitHub 身份
  2. 用户被 GitHub 重定向回您的站点
  3. 您的应用使用用户的访问令牌访问 API

注意:调用请求标识时,请确保需要 API 调用所需的范围。在这种情况下,由于缺少范围,令牌没有对 repo 的适当权限而收到 404。

有关 oauth API 调用的更多信息, 请参见https://developer.github.com/apps/building-oauth-apps/authorizing-oauth-apps/


推荐阅读