无法通过node.js运行远程Google Apps脚本应用

时间:2018-06-01 00:03:21

标签: node.js google-apps-script google-cloud-platform google-api-nodejs-client google-apps-script-api

我在运行以下node.js时遇到问题,无法远程执行Google Apps脚本。 Google Apps脚本本身非常简单,只是:

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

我正在尝试远程测试运行GAS。我使用的脚本如下:

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
  });  
}

出现了很多错误,但最初有这样的错误:

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)

我应该澄清一下,我正在使用带有Ubuntu 18的Google Cloud Platform,其中包含最新的一切,包括节点。 OAuth在开始时工作正常,GAS连接到云平台项目,该项目包含所有需要启用的API,并且client_secret.json已正确设置并位于正确的位置。我已经遵循了这个tutorial,只是按照教程本身,一切正常。只有当我将最后一部分更改为实际运行特定脚本时才会出现错误。如果我像平常一样运行它,简单的GAS本身也可以正常工作。

1 个答案:

答案 0 :(得分:0)

在这里对其进行了基本说明:https://developers.google.com/apps-script/guides/cloud-platform-projects#accessing_an_apps_script_cloud_platform_project

虽然有一个限制,但这也可能是entity not found的原因:

  

团队驱动器中驻留的脚本的Cloud Platform项目可能无法访问。

如何将var scriptId = 'ID';替换为实际的scriptId?

甚至const SCRIPT_ID = '9t453c9zmt5fz792t5z7m9t4...';