บริการ Cloud Identity Groups ขั้นสูง

บริการ Cloud Identity Groups (CIG) ขั้นสูงช่วยให้คุณใช้ CIG API ใน Apps Script ได้

ข้อมูลอ้างอิง

ดูข้อมูลโดยละเอียดเกี่ยวกับบริการนี้ได้ที่เอกสารอ้างอิงสำหรับ CIG API เช่นเดียวกับบริการขั้นสูงทั้งหมดใน Apps Script บริการ CIG ขั้นสูง ใช้ออบเจ็กต์ เมธอด และพารามิเตอร์เดียวกันกับ API สาธารณะ ดูข้อมูลเพิ่มเติมได้ที่วิธีกำหนดลายเซ็นของเมธอด

โค้ดตัวอย่าง

ฟังก์ชันตัวช่วยต่อไปนี้ใช้ API เวอร์ชัน v1

สร้างกลุ่ม

หากต้องการสร้าง Google Group ให้เรียกใช้ groups.create ด้วยอินสแตนซ์ของทรัพยากรกลุ่มใหม่ อินสแตนซ์กลุ่มต้องมี groupKey, parent และ label ตั้งค่าเป็น cloudidentity.googleapis.com/groups.discussion_forum นอกจากนี้ คุณยังต้องตั้งค่าพารามิเตอร์ initialGroupConfig ซึ่งกำหนด เจ้าของเริ่มต้นของกลุ่มด้วย คุณใช้ค่าต่อไปนี้กับพารามิเตอร์นี้ได้

WITH_INITIAL_OWNER: ทำให้ผู้ที่ส่งคำขอเป็นเจ้าของกลุ่ม

EMPTY: สร้างกลุ่มที่ไม่มีเจ้าของเริ่มต้น คุณจะใช้ค่านี้ได้ก็ต่อเมื่อเป็นผู้ดูแลระบบขั้นสูงของ Google Workspace หรือผู้ดูแลระบบกลุ่ม ดูข้อมูลเพิ่มเติม เกี่ยวกับบทบาทใน 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 ให้เรียกใช้ 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);
  }
}