0

我在运行以下 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 的谷歌云平台,所有内容都是最新的,包括节点。OAuth 一开始工作得很好,GAS 连接到云平台项目,该项目启用了所有需要的 API,并且client_secret.json设置正确并位于正确的位置。我已按照本教程进行操作,并且仅按照教程进行操作,一切正常。只有当我将最后一部分更改为实际运行特定脚本时才会出现错误。如果我像往常一样运行它,简单的 GAS 本身也可以正常工作。

4

1 回答 1

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...';

于 2018-08-25T21:05:59.333 回答