บริการ Cloud Identity Groups (CIG) ขั้นสูงช่วยให้คุณใช้ CIG API ใน Apps Script ได้
ข้อมูลอ้างอิง
ดูรายละเอียดเกี่ยวกับบริการนี้ได้ในเอกสารอ้างอิงของ CIG API บริการ CIG ขั้นสูงจะใช้ออบเจ็กต์ วิธีการ และพารามิเตอร์เดียวกับ API สาธารณะเช่นเดียวกับบริการขั้นสูงทั้งหมดใน Apps Script ดูข้อมูลเพิ่มเติมได้ที่วิธีกำหนดลายเซ็นเมธอด
โค้ดตัวอย่าง
ฟังก์ชันตัวช่วยต่อไปนี้ใช้ API เวอร์ชัน v1
สร้างกลุ่ม
หากต้องการสร้าง Google Group ให้เรียกใช้ groups.create
ด้วยอินสแตนซ์ของทรัพยากรกลุ่มใหม่ อินสแตนซ์กลุ่มต้องมี groupKey
, parent
และ label
ตั้งค่าเป็น cloudidentity.googleapis.com/groups.discussion_forum
นอกจากนี้ คุณยังต้องตั้งค่าพารามิเตอร์ initialGroupConfig
ซึ่งจะกำหนดเจ้าของกลุ่มเริ่มต้นด้วย คุณใช้ค่าต่อไปนี้กับพารามิเตอร์นี้ได้
WITH_INITIAL_OWNER
: กำหนดให้บุคคลที่ส่งคำขอเป็นเจ้าของกลุ่ม
EMPTY
: สร้างกลุ่มที่ไม่มีเจ้าของเริ่มต้น คุณจะใช้ค่านี้ได้ก็ต่อเมื่อคุณเป็นผู้ดูแลระบบขั้นสูงของ Google Workspace หรือผู้ดูแลระบบ Groups ดูข้อมูลเพิ่มเติมเกี่ยวกับบทบาทใน Google Workspace ได้ที่บทบาทผู้ดูแลระบบที่กำหนดไว้ล่วงหน้า
ตัวอย่างต่อไปนี้แสดงวิธีสร้างกลุ่มเพื่อให้ผู้ใช้เป็นเจ้าของกลุ่ม
const groups = CloudIdentityGroups.Groups;
function createGroup(groupId, parentId, displayName) {
const groupKey = { id: groupId };
const group = {
parent: "customerId/" + parentId,
displayName: displayName,
groupKey: groupKey,
// Set the label to specify creation of a Google Group.
labels: { "cloudidentity.googleapis.com/groups.discussion_forum": "" },
};
const optionalArgs = { initialGroupConfig: "WITH_INITIAL_OWNER" };
try {
const response = groups.create(group, optionalArgs);
console.log(response);
} catch (error) {
console.error(error);
}
}
ค้นหากลุ่ม
หากต้องการค้นหา Google Group ให้เรียกใช้ groups.search
พร้อมสตริงการค้นหา หากต้องการค้นหากลุ่มทั้งหมด ให้ป้อน
label
cloudidentity.googleapis.com/groups.discussion_forum
const groups = CloudIdentityGroups.Groups;
function searchGroup(customer_id) {
const search_query = `parent=='customerId/${customer_id}' && 'cloudidentity.googleapis.com/groups.discussion_forum' in labels`;
const search_group_request = groups.search({ query: search_query });
console.log(JSON.stringify(search_group_request));
}
เพิ่มการเป็นสมาชิกลงในกลุ่ม
หลังจากสร้างกลุ่มแล้ว คุณจะสร้างการเป็นสมาชิกของกลุ่มได้ วิธีนี้ต้องใช้
ทรัพยากร membership
และสตริงทรัพยากร name
ของรายการหลัก คุณดูค่าเดิมได้โดยค้นหากลุ่มผ่านเมธอด lookup
เมธอดตัวช่วยต่อไปนี้แสดงตัวอย่างการเพิ่มการเป็นสมาชิกในกลุ่ม
expiryDetail
เป็นฟิลด์ที่ไม่บังคับซึ่งสามารถเพิ่มเพื่อกำหนดวันหมดอายุของการเป็นสมาชิกได้ ค่าของ preferredMemberKey
คืออีเมลของสมาชิก
const groups = CloudIdentityGroups.Groups;
function createMembership(namespace, groupId, memberKey) {
try {
// Given a group ID and namespace, retrieve the ID for parent group
const groupLookupResponse = groups.lookup({
'groupKey.id': groupId,
'groupKey.namespace': namespace
});
const groupName = groupLookupResponse.name;
// Create a membership object with a memberKey and a single role of type MEMBER
const membership = {
preferredMemberKey: { id: memberKey },
roles: [
{
name: "MEMBER",
expiryDetail: {
expireTime: "2025-10-02T15:01:23Z",
},
},
],
};
// Create a membership using the ID for the parent group and a membership object
const response = groups.Memberships.create(membership, groupName);
console.log(JSON.stringify(response));
} catch (e) {
console.error(e);
}
}
รับการเป็นสมาชิกจากสมาชิก
ใช้เมธอด groups.memberships.searchDirectGroups
เพื่อค้นหาผู้ปกครองโดยตรงของสมาชิก
เมธอดตัวช่วยต่อไปนี้แสดงตัวอย่างการวนซ้ำการเป็นสมาชิกโดยตรงของสมาชิกรายหนึ่ง
const groups = CloudIdentityGroups.Groups;
function searchMemberMemberships(memberId, pageSize) {
try {
let memberships = [];
let nextPageToken = '';
const withinParent = 'groups/-'; // This parameter sets the scope as "all groups"
do {
// Get page of memberships
const queryParams = {
query: `member_key_id == \'${memberId}\'`,
page_size: pageSize,
page_token: nextPageToken,
};
const response = groups.Memberships.searchDirectGroups(withinParent, queryParams);
memberships = memberships.concat(response.memberships);
// Set up next page
nextPageToken = response.nextPageToken;
} while (nextPageToken);
return memberships;
} catch(e) {
console.error(e);
}
}
รับการเป็นสมาชิกจากกลุ่ม
ใช้เมธอด groups.memberships.list
เพื่อแสดงรายชื่อสมาชิกของกลุ่ม
groupId
: รหัสตัวเลขของกลุ่มที่ต้องการแสดงสมาชิก หากต้องการค้นหารหัสของกลุ่มเดียว ให้ใช้เมธอด groups.lookup
หากต้องการดูรหัสกลุ่มทั้งหมดภายใต้ลูกค้าหรือเนมสเปซ ให้ใช้เมธอด groups.list
const groups = CloudIdentityGroups.Groups;
function listGroupMemberships(groupId, pageSize) {
try {
let membershipList = [];
let nextPageToken = '';
// Get group name
const groupName = groups.lookup({'groupKey.id': groupId}).name;
do {
// Get page of memberships
const queryParams = {
pageSize: pageSize,
pageToken: nextPageToken
}
const response = groups.Memberships.list(groupName, queryParams);
membershipList = membershipList.concat(response.memberships);
// Set up next page
nextPageToken = response.nextPageToken;
} while(nextPageToken);
return membershipList;
} catch (error) {
console.error(error);
}
}