Search code examples
restnpmoctokit

octokit-rest node-js: authorising username and password gets "Token passed to createTokenAuth is not a string" error


EDIT: Despite trying to make it work for 5+ hours, I just saw that the method I describe below is actually deprecated. Anyone who's fallen into the trap I did should use access tokens instead (https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token) and then (https://octokit.github.io/rest.js/v18#authentication)

When using the octokit rest package with node js to authenticate github users, the guidance seems to be very straight forward

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

const octokit = new Octokit({
 auth: {
   username: this_username,
   password: this_password,
   on2fa () {
    return prompt('Enter code')
   }
  }
});

Yet when I run this in node js (having set this_username and this_password to my github username and password), I get the following error:

Uncaught Error: [@octokit/auth-token] Token passed to createTokenAuth is not a string
    at Object.createTokenAuth (D:\github\sos\collector-dev\web\node_modules\@octokit\auth-token\dist-node\index.js:39:11)
    at new Octokit (D:\github\sos\collector-dev\web\node_modules\@octokit\core\dist-node\index.js:115:32)
    at new _a (D:\github\sos\collector-dev\web\node_modules\@octokit\core\dist-node\index.js:167:30)
    at new OctokitWithDefaults (D:\github\sos\collector-dev\web\node_modules\@octokit\core\dist-node\index.js:147:9)

Interestingly, when I deliberately put in a wrong password I get the same error message.

Any ideas about any step that I've missed? Or am I misunderstanding what, username and password, octokit is asking for?


Solution

  • As you mention, is a deprecated method and is not recommended to use it. Still, I found a discussion https://github.com/octokit/rest.js/discussions/1881#discussioncomment-81110 on how to make it work (until Github decides to definitely shut down):

    const octokit = new Octokit({
      authStrategy: createBasicAuth,
      auth: {
        username: this_username,
        password: this_password
      }
    });