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.