github-apioctokitgithub-appoctokit-js

Unable to Authenticate as Github App with Octokit/rest


I'm unable to authenticate as an app using Octokit/rest and the documentation as failed me...

Here's my routine for authenticating:

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)
}

Could anybody point me in the right direction?

When I make a request with the client, I just get a 404. I know the repo exists and Github's docs point out that unauthorised requests may result in a 404 if the client has no access.


Solution

  • I've solved it!

    I wasn't passing the right information to the bot. The working function looks like this.

    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
      })
    }
    

    So close!