3

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());

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!

f k
  • 33
  • 5

1 Answers1

3

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.

Sample 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);
}

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.

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Yes, you are correct. My script works but the problem is that I haven't found out how to convert it into a batch request, meaning to process multiple File IDs in each request. I need to process hundreds of files so I was looking into batching as a solution. So far this is what I found [link](https://developers.google.com/drive/v2/web/batch) but it does not include how to perform it with Google Apps Script. – f k Jan 18 '18 at 08:26
  • @f k I'm sorry for my misunderstanding. I would like to study about it. I'm really sorry. – Tanaike Jan 18 '18 at 11:05
  • No problem. Thanks for trying to help. I will also update here if I find an answer. – f k Jan 18 '18 at 12:21
  • @f k Thank you. Also I would like to update when I find it. – Tanaike Jan 18 '18 at 21:50
  • @f k I'm sorry for my late response. I updated my answer. Please confirm it. – Tanaike Jan 19 '18 at 01:17
  • This was very helpful. I just modified it to adapt to drive v2 and it works. Thanks! – f k Jan 19 '18 at 09:10
  • @f k I'm glad your problem was solved. I could study from your question. Thank you, too. – Tanaike Jan 19 '18 at 11:27