Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drive.Permissions.insert (Value) - Drive API, can you use an array under 'value'?

Would it be possible to use an array under 'value' here to prevent me from creating a group alias email address? For example:

userValues = ["[email protected]", "[email protected]", "[email protected]"];

Drive.Permissions.insert({
    'role': 'writer',
    'type': 'user',
    'value': ** userValues ** ,
  },
  folder, {
    'sendNotificationEmails': 'false'
  });
like image 750
user7935276 Avatar asked Feb 21 '26 14:02

user7935276


1 Answers

Here is my understanding:

  • You want to give the permission to a file and folder using multiple email addresses.
    • For example, you want to use an array like userValues = ["[email protected]", "[email protected]", "[email protected]"];
  • You want to achieve this using Google Apps Script.

Issue and workaround:

In the current stage, Drive.Permissions.insert() can create the permission for one email. Unfortunately, the permission cannot be created with the multiple email by one call of Drive.Permissions.insert(). If you want to use the array and Drive.Permissions.insert, in the current stage, it is required to run Drive.Permissions.insert in the for loop.

As a workaround, here, I would like to propose to use the batch request. When the batch request is used, 100 API calls can be done by one API call and it can be run with the asynchronous process.

Pattern 1:

In this pattern, the batch request is run with UrlFetchApp.

Sample script:

Before you run the script, please set the file ID and email addresses. If you want to add the permission to the folder, please set the folder ID to ### of const fileId = "###";.

function myFunction() {
  const fileId = "###";  // Please set the file ID.
  const userValues = ["[email protected]", "[email protected]", "[email protected]"];  // Please set the email addresses.
  
  const resources = userValues.map(e => ({role: "writer", type: "user", 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())
}

Pattern 2:

In this pattern, a Google Apps Script library for the batch request is used.

Sample script:

Before you run the script, please set the file ID and email addresses, and please install the GAS library.

function myFunction() {
  const fileId = "###";  // Please set the file ID.
  const userValues = ["[email protected]", "[email protected]", "[email protected]"];  // Please set the email addresses.
  
  const reqs = userValues.map(e => ({
    method: "POST",
    endpoint: "https://www.googleapis.com/drive/v3/files/" + fileId + "/permissions?sendNotificationEmails=false",
    requestBody: {role: "writer", type: "user", emailAddress: e},
  }));
  const requests = {batchPath: "batch/drive/v3", requests: reqs};
  const res = BatchRequest.Do(requests);
  console.log(res.getContentText())
}

Note:

  • Please enable V8 at the script editor.
  • In above script, as a sample script, the maximum number of requests is 100. If you want to request over 100, please modify above script. Please be careful this.

References:

  • Batch request
  • Permissions: create
  • BatchRequest of GAS library
like image 71
Tanaike Avatar answered Feb 24 '26 15:02

Tanaike