javascript - GitHub API 401 Node 中的错误凭证但不是 curl
问题描述
我正在尝试使用@octokit/rest
. 如果我使用以下 Curl,我会得到 API 的 URL 列表:
curl -u "my@email.com" https://api.github.<my domain>.com
当我运行它时,系统会提示我输入密码,在其中输入我的个人访问令牌。
但是,如果我在节点应用程序中使用相同的详细信息,我会收到 401 Bad Credentials 响应。这就是我配置身份验证的方式:
octokit.authenticate({
baseUrl: 'https://api.github.<my domain>.com',
type: 'basic',
username: 'my@email.com',
password: '<my personal access token>'
});
根据文档,我认为这应该可行。任何人都可以告诉为什么它没有?
这是我得到的完整 HTTP 响应:
{ HttpError: {"message":"Bad credentials","documentation_url":"https://developer.github.com/v3"}
at response.text.then.message (/node_modules/@octokit/rest/lib/request/request.js:72:19)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
name: 'HttpError',
code: 401,
status: undefined,
headers:
{ 'access-control-allow-origin': '*',
'access-control-expose-headers': 'ETag, Link, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval',
connection: 'close',
'content-length': '83',
'content-security-policy': 'default-src \'none\'',
'content-type': 'application/json; charset=utf-8',
date: 'Mon, 10 Sep 2018 13:20:12 GMT',
'referrer-policy': 'origin-when-cross-origin, strict-origin-when-cross-origin',
server: 'GitHub.com',
status: '401 Unauthorized',
'strict-transport-security': 'max-age=31536000; includeSubdomains; preload',
'x-content-type-options': 'nosniff',
'x-frame-options': 'deny',
'x-github-media-type': 'github.v3; format=json',
'x-github-request-id': 'C8BA:76CE:24B2BB3:45F82A2:5B966F8B',
'x-ratelimit-limit': '10',
'x-ratelimit-remaining': '9',
'x-ratelimit-reset': '1536585672',
'x-runtime-rack': '0.039959',
'x-xss-protection': '1; mode=block' } }
附加信息
这似乎是我的问题发生的地方:
let response = await method({
q: "repo:" + repoOrg + "/" + repoName + " is:issue",
per_page: 100
});
这类似于 npm 页面上的示例。身份验证是否可能在某种程度上没有得到应用,如果是这样,我如何确保它确实得到应用?
解决方案
为了使用令牌进行身份验证,您应该将身份验证类型设置为token
并生成令牌
username
同时接受电子邮件和用户名
完整的工作示例:
const octokit = require('@octokit/rest')();
octokit.authenticate({
type: 'token',
username: 'my username',
token: 'my token'
});
octokit.activity.getStarredRepos().then(results => {
console.log(results);
});
推荐阅读
- c++ - C++ 错误代码:内存位置的 std::bad_alloc
- python - 修改列表列表中的项目,也会更改其他列表相同的项目位置
- amazon-web-services - CloudFormation 可以部署到默认子网吗?
- pandas - date_range 仅在每个月的特定日期,在 pandas
- slack - 如何将 Slack 工作区从移动设备同步到桌面?
- reactjs - React Hoc 函数返回类
- mysql - 添加新帖子后如何更新我的新闻提要
- linux - 为什么当我在命令中使用变量时它会停止工作?|| 外壳脚本
- java - 如何安排任务调用随后将调用 JSON API 的方法
- django - 在两个单独的模型字段 drf 中保存一个值