node.jsgithub-apiprobotgithub-appoctokit-js

Token passed to createTokenAuth is not a string : octokit, auth-token


I am trying to build a GitHub App and following the https://probot.github.io/docs/ and https://octokit.github.io/rest.js/v17#authentication. It is basically a nodejs app.

I have no experience working with nodejs or typescript and not even the probot framework.

The PRIVATE_KEY_PATH is in the .env file as follows:

PRIVATE_KEY_PATH=my-app.2020-04-03.private-key.pem

The .pem file is in the root directory of the project

The typeof prints string : -------------TypeOf token ---- string

index.js

/**
 * This is the main entrypoint to your Probot app
 * @param {import('probot').Application} app
 */



        const{Octokit} = require("@octokit/rest");
        const{createAppAuth} = require("@octokit/auth-token");

         console.log('PRIVATE_KEY',process.env.PRIVATE_KEY_PATH);
         console.log('-------------TypeOf token ----', typeof process.env.PRIVATE_KEY_PATH)

         const appOctokit = new Octokit({
           authStrategy:createAppAuth,
           auth:{
             id:12345,
             privateKey: process.env.PRIVATE_KEY_PATH,
            //  privateKey: 'token ${process.env.PRIVATE_KEY_PATH}'
            }
         });

I keep getting below error :

10:52:51.166Z ERROR probot: [@octokit/auth-token] Token passed to createTokenAuth is not a string

Not able to find much help on this topic over teh internet. I even tried navigating the code of octokit https://github.com/octokit/auth-token.js/blob/master/src/index.ts and it seems I am doing nothing wrong in my code.

There are not much resources to refer for issues for GitHub Apps or probot framework apart from the documentations. StackOverflow too ha just about 20-30 questions related to GitHub Apps or probot framework.

EDIT 1 : START

Running below code :

/**
 * This is the main entrypoint to your Probot app
 * @param {import('probot').Application} app
 */

 const{Octokit} = require("@octokit/rest");

 const{createAppAuth} = require("@octokit/auth-token");

 console.log('PRIVATE_KEY',process.env.PRIVATE_KEY_PATH);
 console.log('-------------TypeOf token ----', typeof process.env.PRIVATE_KEY_PATH)

produces a below error:

ERROR probot: appFn is not a function
  TypeError: appFn is not a function

EDIT 1 : END


Solution

  • If you use Probot, you don't need to load your own @octokit/rest or any of the @octokit/auth-* packages, it's all built into Probot

    Did you try the example code shown on https://probot.github.io/

    module.exports = app => {
      app.on('issues.opened', async context => {
        const params = context.issue({
          body: 'Hello World!'
        })
        await context.github.issues.createComment(params)
      })
    }
    

    Probot will automatically read the contents of your .env file, look for the PRIVATE_KEY_PATH environment variable, read out the contents of the file at that location, and setup the JWT/installation authentication for you