W
W
Wynell_ru2020-01-25 12:58:29
Google
Wynell_ru, 2020-01-25 12:58:29

Node.JS, Trying to get a token from Google API - how to fix invalid_grant error?

Here is the code

const { google } = require('googleapis')
  , yaml = require('yamljs')
  , inquirer = require('inquirer')
  , open = require('open');

const credentials = yaml.load('./credentials.yml').web;

const auth = new google.auth.OAuth2({
  clientId: credentials.client_id,
  clientSecret: credentials.client_secret,
  redirectUri: credentials.redirect_uris[0]
});
async function main() {
  let URL = auth.generateAuthUrl({
    access_type: 'offline',
    scope: [
      'https://www.googleapis.com/auth/forms',
    ]
  });
  let { in_browser } = await inquirer.prompt({
    type: 'confirm',
    name: 'in_browser',
    message: 'Open the URL in browser?'
  });
  if (in_browser) open(URL);
  else console.log(`Open this URL to authorize this script: ${URL}`);
  let { code } = await inquirer.prompt({
    type: 'input',
    name: 'code',
    message: 'Enter the code you got now:'
  });
  let token = await auth.getToken(code);
  console.log(token);
}

main();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikita, 2020-01-25
@Wynell_ru

Similar situation here

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question