Search code examples
google-apps-scripthttp-status-code-404urlfetchbatch-request

https://www.googleapis.com/batch returns error 404


I was trying to create Apps Script web app, which would allow me to share multiple files with multiple users using a solution outlined here

Whenever I tried running the function provided within the linked solution above (the first solution using UrlFetchApp) it always ended in error (code 404)

From the Logger and debugger, i can see that it has trouble reaching the UrlFetchApp.fetch() sequence.

Snippets of my GS Script with only minor modifications from the original link provided above:

var userEmailsArray = ["[email protected]"]; // sample placeholder
var fileId = "File-ID"; // sample placeholder
function changePermissionBatch(role = "reader", type = "user") {
    const resources = userEmailsArray.map(e => ({role: role, type: type, emailAddress: e}));
    const boundary = "xxxxxxxxxx";
    const payload = resources.reduce((s, e, i) => {
      s += "Content-Type: application/http\r\n" +
        "Content-ID: " + i + "\r\n\r\n" +
        "POST https://www.googleapis.com/drive/v3/files/" + fileId + "/permissions?sendNotificationEmails=false" + "\r\n" +
        "Content-Type: application/json; charset=utf-8\r\n\r\n" +
        JSON.stringify(e) + "\r\n" +
        "--" + boundary + "\r\n";
      return s;
    }, "--" + boundary + "\r\n");
    const params = {
      method: "post",
      contentType: "multipart/mixed; boundary=" + boundary,
      payload: payload,
      headers: {Authorization: "Bearer " + ScriptApp.getOAuthToken()},
    };
    const res = UrlFetchApp.fetch("https://www.googleapis.com/batch", params);
    console.log(res.getContentText())
  }

Am I missing something here, or are there any recent policies / updates which may cause the request to fail that I may not be aware of?

The logs provided aren't of much help either since it's a shortened HTTP response, and I've looked around and found no way to conclude what was wrong based on those logs, and I'm basically at a loss here.


Solution

  • I found the answer, thanks to Logan for pointing out the deprecated section. I went out and found a blog post by Tanaike (the person who provided the solution within my referenced link on my original question)

    changed and updated the following line from:

    const res = UrlFetchApp.fetch("https://www.googleapis.com/batch", params);
    

    into:

    const res = UrlFetchApp.fetch("https://www.googleapis.com/batch/drive/v3", params);
    

    The function now works as intended, and the log returns no errors, so problem solved.

    Source:

    Link 1 - Blog Post with sample line containing the specific batchPath required for the code to work

    Link 2 - Official doc / announcements of the issue