Manuel
Manuel

Reputation: 55

Add member to google groups using Google Apps Script

I am looking for a method to add an user to my own google-group (at least to send him an invitation) from google apps script.

The snippet is:

  var options = {  "method"  : "POST",
        "payload" : {"email": email,"role": "MEMBER"},   
        "muteHttpExceptions": true};

  var result = UrlFetchApp.fetch("https://www.googleapis.com/admin/directory/v1/groups/" + respGroup+"/members?key=" + key, options);

But the response is:

{
  "error": {
    "errors": [{
      "domain": "global",
      "reason": "required",
      "message": "Login Required",
      "locationType": "header",
      "location": "Authorization"
    }],
  "code": 401,
  "message": "Login Required"
  }
}

I understood that the problem could be the OAuth authentication, but how do I do it?

Upvotes: 1

Views: 5993

Answers (1)

Serge insas
Serge insas

Reputation: 46812

You can do that easily using the AdminDirectory API (extended Google services that should be activated in the ressource tab) enter image description here

The code is as simple as this :

function addGroupMember(userEmail,groupEmail) {
  var member = {
    email: userEmail,
    role: "MEMBER"
  };
  member = AdminDirectory.Members.insert(member, groupEmail);
  Logger.log("User %s added as a member of group %s.", userEmail, groupEmail);
}

Upvotes: 2

Related Questions