首页 > 解决方案 > 使用 Octokit 登录 Github 的 NodeJS 返回错误凭据

问题描述

我正在开发一个使用 Octokit 从 Github API 返回数据的项目。它返回 401 Bad Credentials,我不确定如何调试问题。

在文档中,它说用 '\n' 分隔私钥行并将其全部粘贴到代码的同一行中。因为我不想将私钥存储在代码中,所以我使用 .pem 文件从单独的 .pem 文件中加载密钥fs.readFileSync(githubCert).toString()。这是加载私钥的正确方法吗?

我尝试重新创建我的 .pem 文件,以文档描述的方式将其包含在代码中(也如上所述)并尝试使用“令牌”而不是“承载者”(我知道这不应该工作,但值得一试射击)。

我的问题是这是正确的做法吗?调试这种错误的正确过程是什么?

这是我的代码:

我正在创建一个 Octokit 应用程序并使用以下方法获取 JWT 令牌:

const app = new App({ id: process.env.GITHUB_APP_ID, privateKey: fs.readFileSync(githubCert).toString() })
const jwt = app.getSignedJsonWebToken()

并从 Github 请求用户,我使用:

export async function getUser(username) {
    return await request('GET /users/:username', {
        username: username,
        headers: {
            authorization: `Bearer ${jwt}`,
            accept: 'application/vnd.github.machine-man-preview+json',
        },
    })
}

来自服务器的响应是:

{ HttpError: Bad credentials
    at response.text.then.message ([PATH TO FOLDER]/embeddable-github-cards/node_modules/@octokit/request/dist-node/index.js:66:23)
    at process._tickCallback (internal/process/next_tick.js:68:7)
  name: 'HttpError',
  status: 401,
  headers:
   { 'access-control-allow-origin': '*',
     'access-control-expose-headers':
      'ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type',
     connection: 'close',
     'content-length': '83',
     'content-security-policy': 'default-src \'none\'',
     'content-type': 'application/json; charset=utf-8',
     date: 'Fri, 24 Jan 2020 21:34:39 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.machine-man-preview; format=json',
     'x-github-request-id': 'CDBD:5718:1C15F98:32D0E0F:5E2B62EC',
     'x-ratelimit-limit': '60',
     'x-ratelimit-remaining': '57',
     'x-ratelimit-reset': '1579905160',
     'x-xss-protection': '1; mode=block' },
  request:
   { method: 'GET',
     url: 'https://api.github.com/users/defunkt',
     headers:
      { accept: 'application/vnd.github.machine-man-preview+json',
        'user-agent':
         'octokit-request.js/5.3.1 Node.js/10.16.3 (macOS Mojave; x64)',
        authorization: 'Bearer [REDACTED]' } },
  documentation_url: 'https://developer.github.com/v3' }

如果您需要更多代码,这里是存储库的链接:https ://github.com/robert-harbison/embed-cards

标签: javascriptnode.jsexpressoctokit-js

解决方案


好的,所以我已经弄清楚如何验证 GitHub api。我需要停止使用 Octokit 应用程序,只使用我的个人 GitHub 令牌使用 Octokit 请求。为此,我将我的个人令牌添加到环境变量中,并将请求标头更改为如下:

export async function getUser(username) {
    return await request('GET /users/:username', {
        username: username,
        headers: {
            Authorization: `token ${process.env.GITHUB_TOKEN}`,
            Accept: 'application/vnd.github.machine-man-preview+json',
        },
    })
} 

推荐阅读