0

I am having a problem running the following node.js to remotely execute a Google Apps script. the Google Apps script in itself is very simple, it is just:

function addText(){
  SpreadsheetApp.openById('ID').getSheetByName('Sheet1').getRange('A1').setValue('test500');
}

I am trying to test running GAS remotely. The script I am using is the following:

const fs = require('fs');
const readline = require('readline');
const {google} = require('googleapis');

const SCOPES = ['https://www.googleapis.com/auth/script.projects', 'https://www.googleapis.com/auth/spreadsheets', 'https://www.googleapis.com/auth/drive'];
const TOKEN_PATH = 'credentials.json';


fs.readFile('client_secret.json', (err, content) => {
  if (err) return console.log('Error loading client secret file:', err);
  authorize(JSON.parse(content), callAppsScript);
});

function authorize(credentials, callback) {
  const {client_secret, client_id, redirect_uris} = credentials.installed;
  const oAuth2Client = new google.auth.OAuth2(client_id, client_secret, redirect_uris[0]);

  fs.readFile(TOKEN_PATH, (err, token) => {
    if (err) return getAccessToken(oAuth2Client, callback);
    oAuth2Client.setCredentials(JSON.parse(token));
    callback(oAuth2Client);
  }); 
}

function getAccessToken(oAuth2Client, callback) {
  const authUrl = oAuth2Client.generateAuthUrl({
    access_type: 'offline',
    scope: SCOPES,
  }); 
  console.log('Authorize this app by visiting this url:', authUrl);
  const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
  }); 
  rl.question('Enter the code from that page here: ', (code) => {
    rl.close();
    oAuth2Client.getToken(code, (err, token) => {
      if (err) return callback(err);
      oAuth2Client.setCredentials(token);
      fs.writeFile(TOKEN_PATH, JSON.stringify(token), (err) => {
        if (err) console.error(err);
        console.log('Token stored to', TOKEN_PATH);
      }); 
      callback(oAuth2Client);
    }); 
  }); 
}

function callAppsScript(auth) {
  var scriptId = 'ID';
  const script = google.script({version: 'v1', auth});
  script.scripts.run({
    auth: auth,
    resource: {
      function: "addText",
      devMode:true
    },  
    scriptId: scriptId
  });  
}

A lot of errors come out, but at the beginning there is this:

Error: Requested entity was not found.
  at createError (.../node_modules/axios/lib/core/createError.js:16:15)
  at settle (/home/jasonjurotich/node_modules/axios/lib/core/settle.js:18:12)
  at IncomingMessage.handleStreamEnd (.../node_modules/axios/lib/adapters/http.js:201:11)
  at IncomingMessage.emit (events.js:187:15)
  at endReadableNT (_stream_readable.js:1090:12)
  at process._tickCallback (internal/process/next_tick.js:63:19)

I should clarify that I am using Google Cloud Platform, with Ubuntu 18, with everything up to date, including node. OAuth is working just fine at the beginning and the GAS is connected to the Cloud Platform Project, which has all the APIs that are needed enabled, and the client_secret.json correctly set and in the right place. I have followed this tutorial and just following the tutorial by itself, everything works fine. It is only when I change the last part to actually run a specific script that the errors come out. The simple GAS itself also works fine if I run it like normal.

tehhowch
  • 9,645
  • 4
  • 24
  • 42
Jason Jurotich
  • 441
  • 4
  • 24
  • Possible duplicate of ["Requested entity was not found." - Apps Script Execution API error](https://stackoverflow.com/questions/49206863/requested-entity-was-not-found-apps-script-execution-api-error) – Martin Zeitler Aug 25 '18 at 21:13

1 Answers1

0

here it is basically explained: https://developers.google.com/apps-script/guides/cloud-platform-projects#accessing_an_apps_script_cloud_platform_project

while there is one limitation to it, which could also be a reason for entity not found:

The Cloud Platform projects of scripts residing in Team Drives may not be accessible.

how about replacing var scriptId = 'ID'; with an actual scriptId ?

or even const SCRIPT_ID = '9t453c9zmt5fz792t5z7m9t4...';

Martin Zeitler
  • 1
  • 19
  • 155
  • 216