GitHub API v3:创建问题时出现 404 错误
GitHub API v3: Getting 404 error on creating issues
我正在关注 GitHub Docs 尝试获取问题列表和 post 个问题。
我已经使用
成功获取了问题列表
GET https://api.github.com/repos/wheatup/wheatup.github.io/issues
但是当我尝试 post 回购问题时,我得到了一个 404 错误并遵循 body:
{
"message": "Not Found",
"documentation_url": "https://docs.github.com/rest/reference/issues#create-an-issue"
}
这是我的 post 请求:
URL
POST https://api.github.com/repos/wheatup/wheatup.github.io/issues
Headers
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
Body
{
title: "Test",
body: "test content",
labels: [],
assignees: [],
milestone: 1
}
这就是我post请求的方式:
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'
}
});
My repo 是 public,我是不是漏掉了什么?任何帮助将不胜感激!
您似乎缺少请求中的 github 标记。在添加不记名令牌之前,我在本地收到 404。然后我得到 401,因为我没有使用实际的不记名令牌来访问您的回购协议。所以一旦你添加了那部分,一切都应该有效。
解决方案一:
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,
});
编辑
解决方案三: -> 上面提供的问题的实际解决方案
处理 OAuth 应用程序时,需要采取一些步骤
- 用户被重定向以请求他们的 GitHub 身份
- GitHub
将用户重定向回您的站点
- 您的应用使用用户的访问令牌
访问API
注意:调用请求标识时,请确保需要 API 调用所需的范围。在这种情况下,收到 404 是因为令牌没有适当的回购权限,因为范围丢失。
有关 oauth API 调用的更多信息可在此处找到
https://developer.github.com/apps/building-oauth-apps/authorizing-oauth-apps/
我正在关注 GitHub Docs 尝试获取问题列表和 post 个问题。
我已经使用
成功获取了问题列表GET https://api.github.com/repos/wheatup/wheatup.github.io/issues
但是当我尝试 post 回购问题时,我得到了一个 404 错误并遵循 body:
{
"message": "Not Found",
"documentation_url": "https://docs.github.com/rest/reference/issues#create-an-issue"
}
这是我的 post 请求:
URL
POST https://api.github.com/repos/wheatup/wheatup.github.io/issues
Headers
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
Body
{
title: "Test",
body: "test content",
labels: [],
assignees: [],
milestone: 1
}
这就是我post请求的方式:
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'
}
});
My repo 是 public,我是不是漏掉了什么?任何帮助将不胜感激!
您似乎缺少请求中的 github 标记。在添加不记名令牌之前,我在本地收到 404。然后我得到 401,因为我没有使用实际的不记名令牌来访问您的回购协议。所以一旦你添加了那部分,一切都应该有效。
解决方案一:
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,
});
编辑
解决方案三: -> 上面提供的问题的实际解决方案
处理 OAuth 应用程序时,需要采取一些步骤
- 用户被重定向以请求他们的 GitHub 身份
- GitHub 将用户重定向回您的站点
- 您的应用使用用户的访问令牌 访问API
注意:调用请求标识时,请确保需要 API 调用所需的范围。在这种情况下,收到 404 是因为令牌没有适当的回购权限,因为范围丢失。
有关 oauth API 调用的更多信息可在此处找到 https://developer.github.com/apps/building-oauth-apps/authorizing-oauth-apps/