Search code examples
google-apps-scriptgoogle-groups

I am trying to extract a list of all members of a Google Groups, including in subGroups, but I can not ignore subgroups i do not have access to


I'm using Apps script to get a list of all members in a Google Groups. I'm using a recursive function to get as deep as necessary in subGroups, however I must deal with the fact that i might not have access to all subGroups within the group. My goal is to ignore the subGroups I do not have access to. For example, I start with group A, it has two subGroups B and C. I do not have access to B. I want to return a list of all members of A and C. My code only returns A members.

The issue is that when I want to get the list of subGroups, it returns an error. Here is the code :

function getAllMembers(group, groupEmail, members = []) {
  Logger.log(group.getEmail())
  try {
    // Ajouter les membres directs du groupe actuel
    try {
      members.push(...group.getUsers().map(user => [user.getEmail(), groupEmail]));
    } catch (error) {
      console.warn(`Accès refusé aux membres du groupe ${group.getEmail()} : ${error.message}`);
    }

    // Parcourir les sous-groupes
    const subGroups = [];
    try {
      Logger.log(...group.getGroups())
      subGroups.push(...group.getGroups());
    } catch (error) {
      console.warn(`Accès refusé aux sous-groupes de ${group.getEmail()} : ${error.message}`);
    }

    for (const subGroup of subGroups) {
      members.push(...getAllMembers(subGroup, groupEmail, []));
    }
  } catch (error) {
    console.error(`Erreur lors de l'accès au groupe ${group.getEmail()} : ${error.message}`);
  }

  return members;
}

The error happens when i call subGroups.push(...group.getGroups()); . The error is : Cannot find a group named: 'email of the group'. Thanks for your help !


Solution

  • It's not clear in the method documentation, but it's not possible to get the groups if you don't have permission to list the members of the subgroups too.

    There's a feature request about it that you can vote on.

    If you happen to be using workspace, there's the admin sdk and cloud identity options, but for consumer groups the only way to get the list is via the web interface.