简体   繁体   English

如何在 Node.js 中验证 GitHub 应用程序?

[英]How do you authenticate a GitHub App in Node.js?

I've created a new GitHub App and I'm trying to authenticate from Node.我创建了一个新的 GitHub 应用程序,我正在尝试从 Node.js 进行身份验证。 I'm new to GitHub Apps, for example, I don't know what "installationId" is supposed to be.例如,我是 GitHub Apps 的新手,我不知道“installationId”应该是什么。 Is that the App ID?那是应用ID吗?

I successfully get a token using a private key, but when I try using the API, I get an error both from Node and from curl.我使用私钥成功获取了令牌,但是当我尝试使用 API 时,我收到来自 Node 和 curl 的错误。

import { createAppAuth } from '@octokit/auth-app';
import { request } from '@octokit/request';

const privateKey = fs.readFileSync(__dirname + 'file.pem');
const auth = createAppAuth({
 id: 1,
 privateKey,
 installationId: 12345,
 clientId: 'xxx.xxxxxx',
 clientSecret: 'xxxxxx',
});

const appAuthentication = await auth({ type: 'app' });
console.log(`Git repo token: ` + appAuthentication.token);

const result = await request('GET /orgs/:org/repos', {
      headers: {
                 authorization: 'token ' + appAuthentication.token,
               },
               org: 'orgname',
               type: 'public',
      });
return res.status(200).json({ message: 'Git result: ' + result });

Here is the curl example I tried after getting the token from the Node code above.这是我从上面的 Node 代码中获取令牌后尝试的 curl 示例。

curl -i -X POST -H "Authorization: Bearer xxxxxxxxxx" -H "Accept: application/vnd.github.machine-man-preview+json" https://api.github.com/app

The result in Node: "Git result: Git repo error: HttpError: Bad credentials" Node 中的结果:“Git 结果:Git repo 错误:HttpError:错误的凭据”

Result in curl: { "message": "Integration not found", "documentation_url": " https://developer.github.com/v3 " }结果 curl: { "message": "Integration not found", "documentation_url": " https://developer.github.com/v3 " }

The JSON Web Token (JWT) authentication can only be used for a few endpoints of GitHub's REST API. JSON Web Token (JWT) 身份验证只能用于 GitHub 的 REST API 的几个端点。 Mostly the ones listed on https://developer.github.com/v3/apps/主要是https://developer.github.com/v3/apps/ 上列出的那些

For all others, you need an installation access token.对于所有其他人,您需要一个安装访问令牌。

Can you try this instead?你可以试试这个吗?

const { token } = await auth({ type: "installation" });
const result = await request("GET /orgs/:org/repos", {
  headers: {
    authorization: "token " + token
  },
  org: "orgname",
  type: "public"
});

Note that the installationId option must be set to a valid installation ID.请注意, installationId选项必须设置为有效的安装 ID。 You get the installation ID when you install the GitHub app on github.com.当您在 github.com 上安装 GitHub 应用程序时,您会获得安装 ID。 For example, you can install the WIP app at https://github.com/apps/wip on your account or any organization you have admin access to.例如,您可以通过https://github.com/apps/wip在您的帐户或您具有管理员访问权限的任何组织上安装 WIP 应用程序。 The installation URLs look like this:安装 URL 如下所示:

https://github.com/settings/installations/90210 https://github.com/settings/installations/90210

In the example above, the installation ID is 90210 .在上面的示例中,安装 ID 是90210

To simplify your code, you can use the auth.hook feature in which case the correct authentication will be set automatically based on the URL为了简化您的代码,您可以使用auth.hook功能,在这种情况下,将根据 URL 自动设置正确的身份验证

import { createAppAuth } from "@octokit/auth-app";
import { request } from "@octokit/request";

const privateKey = fs.readFileSync(__dirname + "file.pem");
const auth = createAppAuth({
  id: 1,
  privateKey,
  installationId: 12345
});

const requestWithAuth = request.defaults({
  request: {
    hook: auth.hook
  }
})

const result = await requestWithAuth("GET /orgs/:org/repos", {
  org: "orgname",
  type: "public"
});

See https://github.com/octokit/request.js/#authentication for more examples有关更多示例,请参阅https://github.com/octokit/request.js/#authentication

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM