无法使用 Octokit/rest 验证 Github 应用程序

问题描述 投票:0回答:2

我无法使用 Octokit/rest 作为应用程序进行身份验证,并且文档让我失败了......

这是我的身份验证例程:

async function getGithub({payload}) {
  const auth = createAppAuth({
    id: parseInt(process.env.APP_ID, 10),
    privateKey: getPrivateKey()
  })

  const installationAuth = await auth({
    type: 'installation',
    installationId: payload.installation.id,
  })

  return new Octokit(installationAuth)
}

有人能指出我正确的方向吗?

当我向客户端发出请求时,我只是收到 404。我知道存储库存在,Github 的文档指出,如果客户端没有访问权限,未经授权的请求可能会导致 404。

github-api octokit github-app octokit-js
2个回答
2
投票

我已经解决了!

我没有向机器人传递正确的信息。工作函数如下所示。

async getGithub(context) {
  const auth = createAppAuth({
    id: parseInt(process.env.APP_ID, 10),
    privateKey: getPrivateKey()
  })

  const installationAuth = await auth({
    type: 'installation',
    installationId: context.payload.installation.id,
  })

  return new Octokit({
    auth: installationAuth.token // directly pass the token
  })
}

这么近!


0
投票

任何登陆这里并使用 Octokit 的人,这里有一种更新的方法:

const config = {
  appId: process.env.GITHUB_APP_ID,
  privateKey: base64ToAscii(process.env.GITHUB_PRIVATE_KEY),
  webhooks: {
    secret: process.env.GITHUB_SIGNING_SECRET,
  },
};

const app = new App(config);

// Get installationId from the installed app or a webhook event
const octokit =
  await app.getInstallationOctokit(installationId);
const { data: user } = await octokit.rest.users.getByUsername({
  username: login,
});
© www.soinside.com 2019 - 2024. All rights reserved.