Skip to content

Instantly share code, notes, and snippets.

@ohsawa0515
Last active December 4, 2022 07:57
Show Gist options
  • Save ohsawa0515/ef5736e7baa849975aaf6d000ed16ef9 to your computer and use it in GitHub Desktop.
Save ohsawa0515/ef5736e7baa849975aaf6d000ed16ef9 to your computer and use it in GitHub Desktop.
Cloud Identity APIでGoogleグループのメンバーリストの取得、メンバー追加・削除をするGoogle Apps Script
/**
* ユーザーリストを返す
* @param {string} groupEmail - グループのEmail
* @return {string[]} - ユーザーEmailのリスト
*/
function listMembers(groupEmail) {
let memberships = listMemberships(groupEmail);
var members = [];
for (const membership of memberships) {
// ロールがオブジェクトかつ配列になっているので「,」でジョインした文字列にする
var roles = [];
membership['roles'].forEach(function (value) {
var property = Object.entries(value);
property.forEach(function (v) {
roles.push(v[1]);
});
});
members.push([membership['preferredMemberKey']['id'], roles.join()]);
}
return members;
}
/**
* ユーザーをグループに追加
* @param {string} userEmail - ユーザーのEmail
* @param {string} groupEmail - グループのEmail
* @return {string} - 実行結果
*/
function insertMember(userEmail, groupEmail) {
const url = `https://cloudidentity.googleapis.com/v1/${getGroupId(groupEmail)}/memberships`;
let body = {
"preferredMemberKey": { "id": userEmail },
"roles": {
"name": "MEMBER"
}
}
let response = UrlFetchApp.fetch(url, {
headers: {
Authorization: `Bearer ${getToken()}`
},
method: "POST",
contentType: "application/json",
payload: JSON.stringify(body),
muteHttpExceptions: true
});
console.log(response.getContentText());
return response.getContentText();
}
/**
* ユーザーをグループから削除
* @param {string} userEmail - ユーザーのEmail
* @param {string} groupEmail - グループのEmail
* @return {string} - 実行結果
*/
function deleteMember(userEmail, groupEmail) {
const url = `https://cloudidentity.googleapis.com/v1/${getMembershipId(userEmail, groupEmail)}`
let response = UrlFetchApp.fetch(url, {
headers: {
Authorization: `Bearer ${getToken()}`
},
method: "DELETE",
muteHttpExceptions: true
});
console.log(response.getContentText());
return response.getContentText();
}
function listMemberships(groupEmail) {
let memberships = [];
let response;
let nextPageToken;
do {
response = listMembershipByPageToken(groupEmail, nextPageToken);
memberships = memberships.concat(response["memberships"]);
// debug
console.log(memberships);
nextPageToken = response["nextPageToken"];
if (typeof nextPageToken === 'undefined') {
return memberships;
}
} while (true);
}
function listMembershipByPageToken(groupEmail, pageToken) {
let url = `https://cloudidentity.googleapis.com/v1/${getGroupId(groupEmail)}/memberships`;
if (typeof pageToken !== 'undefined') {
url += `?pageToken=${pageToken}`
}
// debug
// console.log(url);
let response = UrlFetchApp.fetch(url, {
headers: {
Authorization: `Bearer ${getToken()}`
}
});
return JSON.parse(response.getContentText());
}
function getToken() {
return ScriptApp.getOAuthToken();
}
function getGroupId(groupEmail) {
const url = "https://cloudidentity.googleapis.com/v1/groups:lookup";
let response = UrlFetchApp.fetch(url + "?groupKey.id=" + groupEmail, {
headers: {
Authorization: `Bearer ${getToken()}`
}
});
let res_json = JSON.parse(response.getContentText());
return res_json["name"];
}
function getMembershipId(userEmail, groupEmail) {
const memberships = listMemberships(groupEmail);
for (const membership of memberships) {
if (membership['preferredMemberKey']['id'] == userEmail) {
return membership['name'];
}
}
return null;
}
@ohsawa0515
Copy link
Author

ohsawa0515 commented Sep 18, 2022

# example
function myFunction() {
  const groupEmail = "[email protected]";
  const userEmail = "[email protected]";

  // メンバーリストの取得
  var members = listMembers(groupEmail);
  console.log(members);

  // メンバーをGoogleグループに追加
  insertMember(userEmail, groupEmail);

  // Googleグループからメンバーを削除
  deleteMember(userEmail, groupEmail);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment