【发布时间】:2018-10-01 14:00:39
【问题描述】:
我正在尝试编写一个独立的 Google Apps 脚本,该脚本使用 Google Apps Script API 更新许多 Google 表格的绑定脚本内容。
我拥有从模板创建的大约 200 个 Google 表格的表格 ID。我想将这些工作表上的绑定脚本的项目内容更新为与一组主脚本相同。
在使用 urlFetchApp 获取一张纸的绑定脚本的内容作为测试时,我遇到了身份验证错误。错误看起来像:
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 在这种情况下可能有用,我只是不确定如何使用它。谁能指出我正确的方向?
【问题讨论】:
-
您需要在 urlfetch 行中提供授权标头。如果执行用户对文档具有编辑权限,则不需要为此使用库。
-
例如,这个 GAS 库对您的情况有用吗? github.com/tanaikech/ProjectApp2
-
我已经提交了一个 IssueTracker 请求,要求提供一项可以使这项任务变得更加简单的功能:issuetracker.google.com/issues/117105106
标签: google-apps-script google-apps-script-api