如何解决GitHub API v3:在创建问题时遇到404错误 URL 标题身体
我正在关注GitHub Docs,以获取问题列表并发布问题。
我设法使用
获取问题列表GET https://api.github.com/repos/wheatup/wheatup.github.io/issues
但是当我尝试将问题发布到仓库时,出现以下正文错误404:
{
"message": "Not Found","documentation_url": "https://docs.github.com/rest/reference/issues#create-an-issue"
}
这是我的帖子请求:
URL
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',milestone: 1,},{
headers: {
'Authorization': 'token d7fa1e545c*******************31957a97e06','Content-Type': 'application/json','Accept': 'application/vnd.github.v3+json'
}
});
My repo是公开的,我想念什么吗?任何帮助将不胜感激!
解决方法
似乎您在请求中缺少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: '',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',milestone: 1,});
编辑
解决方案3: ->上面提供的问题的实际解决方案
处理OAuth应用时,需要采取一些步骤
- 用户被重定向以请求其GitHub身份
- GitHub将用户重定向回您的站点
- 您的应用使用用户的访问令牌访问API
注意:调用请求标识时,请确保要求进行API调用所需的范围。在这种情况下,由于令牌缺少范围,因此令牌没有对存储库的适当权限,因此收到404。
有关oauth API调用的更多信息,请参见此处 https://developer.github.com/apps/building-oauth-apps/authorizing-oauth-apps/
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。