问题
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