How to automatically grant access to google drive access requests

192 Views Asked by At

I'm distributing a product that is available for download on Google Drive. What happens now is that each person who wants access needs to ask for permission and google drive sends me an access request which I manually have to accept.

I want to automate this process. I found the following script that should make this work:

/*
Purpose of the Script:
Add Shared Drive Access Requestor to Google Group used for ACL
Shared Drive Link: https://drive.google.com/drive/u/2/folders/1EycKD-KKbdjRihYR1zKWpmzI62saAM1Z
*/

function addtoGroup() {
  var interval = 5;    //  if the script runs every 5 minutes; change otherwise
  var emails = [];
  var timeFrom = Math.floor(Date.now()/1000) - 60 * interval;
  var group = GroupsApp.getGroupByEmail("MYEMAIL");
  var threads = GmailApp.search('label: "Access Request" after:' + timeFrom);
  for (var i = 0; i < threads.length; i++) {
    var messages = threads[i].getMessages();
    if (messages.length == 1) {      // only deal with threads with one message
      var ReplyToEmail = messages[0].getReplyTo().match(/([^<]+@[^>]+)/)[1];
      emails.push(ReplyToEmail);
      messages[0].reply("Je hebt nu toegang om het luisterboek te downloaden.",{
        cc:"MYEMAIL"
      })
    }
  }
  for (i=0; i < emails.length; i++) {
    try {
      addMember (emails[i], group);
    }
    catch(e) {
      console.error(e);
      continue;
    }
  }
}

function addMember (email, group) {
  var hasMember = group.hasUser(email);
  Utilities.sleep(1000);

  if(!hasMember) {
    var newMember = {email: email,
    role: "MEMBER",
    delivery_settings: "NONE"};
    }
  
}

I got this script from: https://www.youtube.com/watch?v=xvNb2hRsGxo&t=218s

But I have no clue how to actually connect this to my gmail/google drive. I'm able to create the script but how does the script know to use my google drive and read my gmail account?

Or will this happen automatically if all are on the same workspace account?

1

There are 1 best solutions below

4
Twilight On

SUGGESTION:

Aside from my suggestion I have commented above, I have tried replicating the youtube video you have provided.

After creating file/s to be shared on Shared drives, you can set your personal gmail account as Content Manager or Manager.

Then, in line:

var group = GroupsApp.getGroupByEmail("[google grp]");

you have to create Google group since personal gmail will not work on this.

Add this google group email as manager/content manager.

Then on Services, Add the Admin SDK API so you can use these lines:

if(!hasMember) {
    var newMember = {email: email,
    role: "MEMBER",
    delivery_settings: "NONE"};
    AdminDirectory.Members.insert(newMember, "[google grp]");
  }

These lines will add those people who want access but not a member yet on your google group.

I have tried to run the script in a time-based trigger(every 5 mins).

This will now automatically accept those who request permission to access your file/s.

Reference: Class GroupsApp