3

Cheers Everyone!

I have a Google script which checks if e-mail addresses are members of a group or not by using getUsers() function.

So far:

  • I have activated "Admin SDK Directory Service"
  • I have admin authority

For most of the groups it does it's magic, however I get authorization error in case of some groups.

Error message from Log:

  • "You do not have permission to view the member list for the group: foo@bar"

Any idea what might be the problem? Anything is very much appreciated. Thank you!

Zoru
  • 47
  • 7
  • Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example. – Linda Lawton - DaImTo May 12 '15 at 06:43

1 Answers1

4

The problem is that the GroupsApp service uses the permissions of the GROUP to determine whether or not you can view the members list. The default setting for groups is to restrict this access to owners and managers of the group. So you have two options:

1) Make yourself an owner or manager of the group OR

2) Use the Admin SDK to check for group membership. The Admin SDK allows any super admin to view the list of users in a group. To find out whether a user is a member of a group, you would need to retrieve the group, then iterate through the members list and then compare each member against the user you are looking for:

 function isMember(groupKey,userKey){
    //groupKey: testGroup@yourdomain.com
    //userKey: userEmail@yourdomain.com 

    var memberList = [];

    //Get the members list from the group
    var response = AdminDirectory.Members.list(groupKey);
    memberList = memberList.concat(response.members);
    while (response.nextPageToken){
      response = AdminDirectory.Members.list(groupKey,{pageToken: response.nextPageToken});
      memberList = memberList.concat(response.members);
    }

    if (memberList.length > 1){

      for (var x in memberList){
        if (memberList[x].email == userKey){return true;}
     }
   }
 }

More info Here

Charlie Patton
  • 435
  • 2
  • 12