为什么我不能使用 Axios 通过 GitHub REST API 进行身份验证?
Why can I not authenticate with the GitHub REST API using Axios?
我对 REST 有点陌生..
为了全面披露,我在 Netlify Lambda 函数中 运行 这段代码并通过 netlify-lambda 进行测试。
我的 curl 命令有效:
curl -u "<username>:<password>" https://api.github.com/repos/<username>/<reponame>
但是当我尝试通过 axios 获取请求时,我收到了 404(根据 github 文档,这意味着身份验证问题)。这就是我正在做的(如果没有自定义 headers 也无法工作,我只是在尝试随机的事情)。
axios({
method: "get",
url: `https://api.github.com/repos/${user}/<reponame>/`,
headers: {
Authorization: `Bearer ${githubToken}`,
"Content-Type": "application/json"
},
auth: {
username: user,
password: pass
}
})
.then(res => {
callback(null, {
statusCode: 200,
body: JSON.stringify(res.data)
});
})
.catch(err => {
callback(err);
});
我注意到的一件事是,axios 似乎在获取我的用户名和密码并将它们添加到 url i.g 前面。 https://<username>:<password>@api.github.com/repos/<username>/<reponame>
授权应该这样发送吗?
如果您已有不需要的令牌 user/pass,只需将令牌添加到 header。
我的 URL.
结尾不应该有尾部正斜杠
我对 REST 有点陌生..
为了全面披露,我在 Netlify Lambda 函数中 运行 这段代码并通过 netlify-lambda 进行测试。
我的 curl 命令有效:
curl -u "<username>:<password>" https://api.github.com/repos/<username>/<reponame>
但是当我尝试通过 axios 获取请求时,我收到了 404(根据 github 文档,这意味着身份验证问题)。这就是我正在做的(如果没有自定义 headers 也无法工作,我只是在尝试随机的事情)。
axios({
method: "get",
url: `https://api.github.com/repos/${user}/<reponame>/`,
headers: {
Authorization: `Bearer ${githubToken}`,
"Content-Type": "application/json"
},
auth: {
username: user,
password: pass
}
})
.then(res => {
callback(null, {
statusCode: 200,
body: JSON.stringify(res.data)
});
})
.catch(err => {
callback(err);
});
我注意到的一件事是,axios 似乎在获取我的用户名和密码并将它们添加到 url i.g 前面。 https://<username>:<password>@api.github.com/repos/<username>/<reponame>
授权应该这样发送吗?
如果您已有不需要的令牌 user/pass,只需将令牌添加到 header。
我的 URL.
结尾不应该有尾部正斜杠