【问题标题】:How to make a Drive API batch request with UrlFetchApp in Google Apps Script如何在 Google Apps 脚本中使用 UrlFetchApp 发出 Drive API 批处理请求
【发布时间】:2018-01-17 16:10:56
【问题描述】:

我想知道如何在 Google Apps 脚本中使用 UrlFetchApp 发出 Drive API 批处理请求。我已经阅读了谷歌文档,但我仍然不清楚。我想将下面的代码转换为接受多个文件 ID 的批处理请求。

var url = 'https://www.googleapis.com/drive/v2/files/FILEID/permissions';

var data = {
    "role":"owner",
    "type":"user",
    "value": NEWOWNER
  };

var response = UrlFetchApp.fetch(url, {
  headers: {
    Authorization: 'Bearer ' + AUTHORIZATIONTOKEN
  },
  method: 'post',
  contentType: 'application/json',
  payload: JSON.stringify(data),
});
var result = JSON.parse(response.getContentText());

我尝试使用https://www.googleapis.com/drive/v2/files?id=FILEID1&id=FILEID2/permissions,但它似乎不起作用。我刚刚看到将每个文件请求嵌套到批处理请求的描述,但到目前为止我还没有找到正确执行此操作的语法示例。

一些见解会有所帮助。谢谢!

【问题讨论】:

    标签: google-drive-api google-apps-script


    【解决方案1】:

    这个示例脚本怎么样?当我看到this document 时,我可以发现您要进行批处理请求的API 调用是使用multipart/mixed 发送的。这样,我可以为 Google Apps Script 创建一个示例脚本,如下所示。

    示例脚本:

    function myFunction() {
      var body = [
        {
          method: "POST",
          endpoint: "https://www.googleapis.com/drive/v3/files/### fileId 1 ###/permissions",
          requestBody: {
           "role": "owner",
           "type": "user",
           "emailAddress": NEWOWNER
          }
        },
        {
          method: "POST",
          endpoint: "https://www.googleapis.com/drive/v3/files/### fileId 2 ###/permissions",
          requestBody: {
           "role": "owner",
           "type": "user",
           "emailAddress": NEWOWNER
          }
        }
      ];
    
      var boundary = "xxxxxxxxxx";
      var contentId = 0;
      var data = "--" + boundary + "\r\n";
      for (var i in body) {
            data += "Content-Type: application/http\r\n";
            data += "Content-ID: " + ++contentId + "\r\n\r\n";
            data += body[i].method + " " + body[i].endpoint + "\r\n";
            data += body[i].requestBody ? "Content-Type: application/json; charset=utf-8\r\n\r\n" : "\r\n";
            data += body[i].requestBody ? JSON.stringify(body[i].requestBody) + "\r\n" : "";
            data += "--" + boundary + "\r\n";
      }
      var payload = Utilities.newBlob(data).getBytes();
      var options = {
        method: "post",
        contentType: "multipart/mixed; boundary=" + boundary,
        payload: payload,
        headers: {'Authorization': 'Bearer ' + ScriptApp.getOAuthToken()},
        muteHttpExceptions: true,
      };
      var res = UrlFetchApp.fetch("https://www.googleapis.com/batch", options).getContentText();
      Logger.log(res);
    }
    

    注意:

    • 请根据您的环境修改此示例脚本。
    • 如果您需要更多 API,请将元素添加到“body”数组中。
    • 假设您已经启用 Drive API。
    • Drive API 在一个批处理请求中最多可以使用 100 个调用。这是一个限制。
    • 对于批量请求,每个 API 调用都没有保证顺序。

    在我的环境中,我确认这有效。但是,如果这在您的环境中不起作用,请告诉我。我要修改。

    【讨论】:

    • 是的,你是对的。我的脚本有效,但问题是我还没有找到如何将其转换为批处理请求,这意味着在每个请求中处理多个文件 ID。我需要处理数百个文件,所以我正在研究批处理作为解决方案。到目前为止,这是我找到的 link,但它不包括如何使用 Google Apps 脚本执行它。
    • @f k 很抱歉我的误解。我想研究一下。真的很抱歉。
    • 没问题。感谢您尝试提供帮助。如果我找到答案,我也会在这里更新。
    • @f k 谢谢。我也想在找到它时更新。
    • @f k 很抱歉我的回复迟了。我更新了我的答案。请确认。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-04
    • 2020-08-31
    • 1970-01-01
    • 2021-11-01
    • 2019-12-29
    • 2018-08-13
    • 2014-03-05
    相关资源
    最近更新 更多