Add member to google groups using Google Apps Script

ε祈祈猫儿з 提交于 2020-01-03 02:49:09

问题


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?


回答1:


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

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);
}


来源:https://stackoverflow.com/questions/37231757/add-member-to-google-groups-using-google-apps-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!