如何在Google Apps脚本中使用UrlFetchApp发出Drive API批处理请求 [英] How to make a Drive API batch request with UrlFetchApp in Google Apps Script

查看:101
本文介绍了如何在Google Apps脚本中使用UrlFetchApp发出Drive API批处理请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to know how to make a Drive API batch request with UrlFetchApp in Google Apps Script. I've read the google documentation but it still isn't clear to me. I want to convert the code below into a batch request accepting multiple file IDs.

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,但似乎无法正常工作.我刚刚看到了将每个文件请求嵌套到批处理请求的描述,但是到目前为止,我还没有找到正确执行该请求的语法示例.

I tried using https://www.googleapis.com/drive/v2/files?id=FILEID1&id=FILEID2/permissionsbut it seemed to be not working. I just saw description of nesting each file request to a batch request, but so far I have not found a syntax example for doing it correctly.

一些见识会有所帮助.谢谢!

Some insight would be helpful. Thanks!

推荐答案

该示例脚本如何?当我看到此文档时,我发现您想要的API调用使用multipart/mixed发送批处理请求.这样,我可以为Google Apps脚本创建一个示例脚本,如下所示.

How about this sample script? When I saw this document, I could find that the API calls you want to do batch request are sent using multipart/mixed. By this, I could create a sample script for Google Apps Script as follows.

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调用都不保证有序.
  • Note :

    • Please modify this sample script for your environment.
    • If you want more APIs, please add the elements to the array of "body".
    • It supposes that you have already enabled Drive API.
    • Drive API can be used the maximum of 100 calls in one batch request. This is a limitation.
    • For the batch request, each API call is no guaranteed ordering.
    • 在我的环境中,我确认这是可行的.但是,如果这在您的环境中不起作用,请告诉我.我想修改.

      In my environment, I confirmed that this works. But if this didn't work in your environment, please tell me. I would like to modify.

      这篇关于如何在Google Apps脚本中使用UrlFetchApp发出Drive API批处理请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆