Nightforce2
Nightforce2

Reputation: 1476

How do you leave a googlegroup via the Googlescript API

What I am trying to do is leave a googlegroup by utilizing the API rather then having to click on the group itself. This is not shown in the API documentation located here: Google Groups API

In trying to find a way to leave a group via the API I did find out I was a member of different groups that I did not join but it appears there is no documented way of leaving them. Going here: Enum Role. This allows me to see what role I have within a group and the corresponding code used is below.

There is a Enterprise Admin API for removing a group membership but this is not a Enterprise setup and thus AdminGroupSettings cannot be used in this scope.

function getGroupNames() {

  try {
      var groups = GroupsApp.getGroups();
  }
  catch(err) {
      console.log("Permission error [getGroup]: Cannot access group members list!");
  }
  
  for (var i = 0; i < groups.length; i++) {
    var group = groups[i];
    var currentUser = Session.getActiveUser();
    // https://stackoverflow.com/questions/62801809/in-google-app-scripts-how-can-you-get-the-group-name-with-the-group-email
    // Groups Settings API/AdminGroupSettings
    // var groupName = AdminGroupsSettings.Groups.get('[email protected]').name;
    // console.log("Attempting to get group name: " + groupName + " from address " + groups[i]);
    if (group.getRole(currentUser.getEmail()) == GroupsApp.Role.MEMBER){
        // How to leave group??
        console.log('You are a member of ' + group.getEmail()); 
        continue;
    }
    if (group.getRole(group.getEmail()) == GroupsApp.Role.INVITED){
        // leave group
        console.log('You are invited to ' + group.getEmail());
        continue;
    }
    if (group.getRole(group.getEmail()) == GroupsApp.Role.MANAGER){
        console.log('You are manager of ' + group.getEmail());
        continue;
    }
    if (group.getRole(group.getEmail()) == GroupsApp.Role.OWNER){
        // leave group
        console.log('You are owner of ' + group.getEmail());
        continue;
    }    
      if (group.getRole(group.getEmail()) == GroupsApp.Role.PENDING){
        // leave group
        console.log('You are pending to ' + group.getEmail());
        continue;
    }
  }

}

Is it possible to leave a group via Google Groups API? If so, how?

Upvotes: 1

Views: 124

Answers (1)

Rafa Guillermo
Rafa Guillermo

Reputation: 15357

Answer:

You need to use the Admin SDK Directory API to remove a member from a group.

More Infomation:

The Directory API has a members.delete method which allows you to remove a member from a Google Group.

In Apps Script, you will need to add the AdminDirectory Advanced service to the project, and specify the groupKey of the group and the membersKey of the member you wish to remove in the request:

const groupKey = "XXXXXXXXX"
const memberKey = "YYYYYY"

AdminDirectory.Members.remove(groupKey, memberKey)

A successful deletion will result in an HTTP 204 reponse.

NB: You need to run this code as a domain Admin. It is not possible to remove a member from a Google Group via the API without using the Admin SDK.

Upvotes: 0

Related Questions