W
W
Wynell_ru2020-01-25 14:42:03
Google
Wynell_ru, 2020-01-25 14:42:03

Google API via NodeJS: Error: Requested entity was not found. - how to fix it?

main.js

const { google } = require('googleapis')
  , yaml = require('yaml')
  , authorize = require('./functions/authorize')
  , { readFileSync } = require('fs');

const credentials = yaml.parse(readFileSync('./credentials.yml').toString()).web;

const OAuth2Client = new google.auth.OAuth2({
  clientId: credentials.client_id,
  clientSecret: credentials.client_secret,
  redirectUri: credentials.redirect_uris[0]
});

async function main() {
  let tokens = await authorize(OAuth2Client);
  console.log(await google.script({
    version: 'v1',
    auth: OAuth2Client
  }).scripts.run({
    scriptId: /* script_id */''
  }));

}

main();

authorize.js
const { readFileSync, writeFileSync, existsSync } = require('fs')
  , inquirer = require('inquirer')
  , open = require('open')
  , yaml = require('yaml');

module.exports = async function (auth) {
  let tokens = existsSync('./tokens.yml') && yaml.parse(readFileSync('./tokens.yml').toString());
  if (tokens && tokens.access_token && tokens.refresh_token) {
    auth.setCredentials(tokens)
    return tokens;
  }
  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 result = await auth.getToken(code);
  auth.setCredentials(result.tokens)
  writeFileSync('./tokens.yml', yaml.stringify(result.tokens));
  return result.tokens;
}

The ScriptId is correct, it's exactly
google.script(...).projects.get({scriptId:...}) seems to work, but google.script(...).scripts.run({scriptId:... }) does not work (gives an error from the name)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question