google-apps-scriptgoogle-sheetsgoogle-groups

API call to directory.members.insert failed with error: Resource Not Found: groupKey


I'm working on a script for Google Sheets that adds members to a specific Google Group using emails found in the data. The Google Group seems to be found and the Google Group's email/groupKey is returned correctly. The currentUser in the session (me) is a member of the group (owner). I have enabled the Admin Directory API. In one test, I even called group.getUsers() and all the members of the group are correctly returned. All the values in logging are as expected.

Why is the call to insert still failing? What is the member not added to the google group?

I've included code and log.

Code

function addMembersFromSheet() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("MySheet");
  var emails = sheet.getRange(2,3,sheet.getLastRow()-1,1).getValues();
  Logger.log(emails);
  
  var currentUser = Session.getActiveUser();
  Logger.log(currentUser);
 
  var group = GroupsApp.getGroupByEmail("<mygroup>@googlegroups.com");
    
  if (group.hasUser(currentUser)) {
    Logger.log("You are a member of this group. group is: ");
    Logger.log(group.getEmail()); //correctly finds the group
  
  }
  else {
    Logger.log("You are not a member of this group.");
  }

  for(i = 0; i < emails.length; i++) {
    try {
      addMember(emails[i][0], group);
    }
    catch(e) {
      console.error(e);
      Logger.log(e);
      continue;
    }   
  }
}

function addMember(email, group) {
  
  var hasMember = group.hasUser(email);
  Logger.log(hasMember);
  Utilities.sleep(1000);
  
  if(!hasMember) {
    var newMember = {email: email, 
                     role: "MEMBER",
                     delivery_settings: "NONE"};
    Logger.log(newMember);
    Logger.log(group.getEmail())
    AdminDirectory.Members.insert(newMember, group.getEmail());
  }
}

Logging output(with some changes to private details):

Jul 26, 2020, 3:22:09 PM    Info    [[<testmember>@gmail.com]]
Jul 26, 2020, 3:22:09 PM    Info    <currentUser>@gmail.com
Jul 26, 2020, 3:22:10 PM    Info    You are a member of this group. group is: 
Jul 26, 2020, 3:22:10 PM    Info    <mygroup>@googlegroups.com
Jul 26, 2020, 3:22:10 PM    Info    false
Jul 26, 2020, 3:22:11 PM    Info    {delivery_settings=NONE, role=MEMBER, email=<testmember>@gmail.com}
Jul 26, 2020, 3:22:11 PM    Info    <mygroup>@googlegroups.com
Jul 26, 2020, 3:22:11 PM    Error   { [GoogleJsonResponseException: API call to directory.members.insert failed with error: Resource Not Found: groupKey]
  details: 
   { code: 404,
     message: 'Resource Not Found: groupKey',
     errors: [ [Object] ] },
  name: 'GoogleJsonResponseException' }
Jul 26, 2020, 3:22:11 PM    Info    GoogleJsonResponseException: API call to directory.members.insert failed with error: Resource Not Found: groupKey

Solution

  • You are retrieving a group of a consumer account and try to perform on it methods of A GSuite Admin

    This API gives administrators of G Suite domains (including resellers) the ability to manage devices, groups, users, and other entities in their domains.