google-apps-script - Google Apps 独立脚本使用 Google Apps 脚本 API 更新许多绑定(bind)脚本

标签 google-apps-script google-apps-script-api

我正在尝试编写一个使用 Google Apps Script API 的独立 Google Apps 脚本。更新许多 Google 表格的绑定(bind)脚本内容。

我有大约 200 个根据模板创建的 Google 表格的工作表 ID。我想更新每张工作表上绑定(bind)脚本的项目内容,使其与一组主脚本相同。

在使用 urlFetchApp 获取一张纸的绑定(bind)脚本的内容作为测试时,我遇到了身份验证错误。错误看起来像:

Request failed for
https://script.googleapis.com/v1/projects/<SCRIPTID>/content returned code 401. 
Truncated server response: { "error": { "code": 401, 
"message": "Request is missing required authentication credential.
Expected OAuth 2 access token, login cookie ... 
(use muteHttpExceptions option to examine full response) (line 34, file "AddScriptsToSheets")

我正在使用的测试函数如下所示:

function getSheetScriptContent(sheetId) { 
  var sheet = SpreadsheetApp.openById(sheetId);
  // Make a POST request with a JSON payload.
  // Make a GET request and log the returned content.
  var url = PROJECTS_GET_CONTENT_URL.format(sheetId);
  var response = UrlFetchApp.fetch(url);
  Logger.log(response.getContentText());
}

我认为这个OAuth2 library在这种情况下可能有用,我只是不确定如何使用它。有人能指出我正确的方向吗?

最佳答案

如果您拥有所有文件,则无需使用 OAuth 库或任何特殊代码来获取访问 token 。您可以从 ScriptApp 类获取访问 token 。

var theAccessTkn = ScriptApp.getOAuthToken();

您可能需要手动编辑 appsscript.json list 文件并添加范围:

https://www.googleapis.com/auth/script.projects

appsscript.json

{
  "timeZone": "Yours will display here",
  "dependencies": {
  },
  "webapp": {
    "access": "MYSELF",
    "executeAs": "USER_DEPLOYING"
  },
  "exceptionLogging": "STACKDRIVER",
  "oauthScopes": [
    "https://www.googleapis.com/auth/drive", 
    "https://www.googleapis.com/auth/script.projects", 
    "https://www.googleapis.com/auth/drive.scripts", 
    "https://www.googleapis.com/auth/script.container.ui", 
    "https://www.googleapis.com/auth/script.external_request"
  ],
  "runtimeVersion": "DEPRECATED_ES5"
}

覆盖 Apps 脚本文件的代码:

function updateContent(scriptId,content,theAccessTkn) {
try{
  var options,payload,response,url;

  if (!content) {
    //Error handling function
    return;
  }
  
  if (!theAccessTkn) {
    theAccessTkn = ScriptApp.getOAuthToken();
  }
  
  //https://developers.google.com/apps-script/api/reference/rest/v1/projects/updateContent
  url = "https://script.googleapis.com/v1/projects/" + scriptId + "/content";

  options = {
    "method" : "PUT",
    "muteHttpExceptions": true,
    "headers": {
      'Authorization': 'Bearer ' +  theAccessTkn
     },
    "contentType": "application/json",//If the content type is set then you can stringify the payload
    "payload": JSON.stringify(content)
  };
  
  response = UrlFetchApp.fetch(url,options);      
  //Logger.log('getResponseCode ' + response.getResponseCode())
  //Logger.log("Response content: " + response.getContentText())

} catch(e) {
  console.log("Error: " + e + "\nStack: " + e.stack)
}
};

关于google-apps-script - Google Apps 独立脚本使用 Google Apps 脚本 API 更新许多绑定(bind)脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52592828/

相关文章:

javascript - 超出了 Google 应用脚本的最长执行时间

javascript - 使用 Google Script 在 Google Forms 中提交时获取地理位置

google-apps-script - 按名称driveapp获取文件夹

google-apps-script - 为什么我部署为 API 可执行文件的应用程序脚本返回“权限被拒绝”?

google-apps-script - Apps 脚本执行 API 显示 devMode 错误 : true

gmail - 使用 Google 脚本检查消息是否有标签

google-apps-script - 使用 API 以编程方式创建 Google App 脚本

c# - Google Apps 脚本执行 API 服务授权每小时失败一次

google-apps-script - 从一个 appscript 更新多个 appscript 项目

javascript - Google电子表格javascript宏删除特定元素大于某个值的行