บริการไดเรกทอรี Admin SDK

เพื่อจัดการโดเมน Google Workspace รวมถึงอุปกรณ์ กลุ่ม และผู้ใช้

บริการ Admin SDK Directory ช่วยให้คุณใช้ Directory API ของ Admin SDK ใน Google Apps Script ได้ API นี้ช่วยให้ผู้ดูแลระบบโดเมน Google Workspace (รวมถึงตัวแทนจำหน่าย) สามารถจัดการอุปกรณ์ กลุ่ม ผู้ใช้ และเอนทิตีอื่นๆ ในโดเมนของตนได้

นี่เป็นบริการขั้นสูงที่ต้อง เปิดใช้ก่อนใช้งาน นอกจากนี้ คุณต้องเปิดใช้ Admin SDK ในโดเมนตามที่อธิบายไว้ในเอกสารประกอบข้อกำหนดเบื้องต้นของ API's

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

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

หากต้องการรายงานปัญหาและดูการสนับสนุนอื่นๆ โปรดดู คู่มือการสนับสนุน Admin SDK Directory

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

โค้ดตัวอย่างต่อไปนี้ใช้ เวอร์ชัน 1 ของ API

แสดงรายการผู้ใช้ทั้งหมด

ตัวอย่างนี้แสดงรายการผู้ใช้ทั้งหมดในโดเมนโดยเรียงตามชื่อ

advanced/adminSDK.gs
/**
 * Lists all the users in a domain sorted by first name.
 * @see https://developers.google.com/admin-sdk/directory/reference/rest/v1/users/list
 */
function listAllUsers() {
  let pageToken;
  let page;
  do {
    page = AdminDirectory.Users.list({
      domain: "example.com",
      orderBy: "givenName",
      maxResults: 100,
      pageToken: pageToken,
    });
    const users = page.users;
    if (!users) {
      console.log("No users found.");
      return;
    }
    // Print the user's full name and email.
    for (const user of users) {
      console.log("%s (%s)", user.name.fullName, user.primaryEmail);
    }
    pageToken = page.nextPageToken;
  } while (pageToken);
}

ขอรับผู้ใช้

ตัวอย่างนี้จะดึงข้อมูลผู้ใช้ตามอีเมลและบันทึกข้อมูลทั้งหมดของผู้ใช้เป็นสตริง JSON

advanced/adminSDK.gs
/**
 * Get a user by their email address and logs all of their data as a JSON string.
 * @see https://developers.google.com/admin-sdk/directory/reference/rest/v1/users/get
 */
function getUser() {
  // TODO (developer) - Replace userEmail value with yours
  const userEmail = "liz@example.com";
  try {
    const user = AdminDirectory.Users.get(userEmail);
    console.log("User data:\n %s", JSON.stringify(user, null, 2));
  } catch (err) {
    // TODO (developer)- Handle exception from the API
    console.log("Failed with error %s", err.message);
  }
}

เพิ่มผู้ใช้

ตัวอย่างนี้จะเพิ่มผู้ใช้ใหม่ลงในโดเมน โดยรวมเฉพาะข้อมูลที่จำเป็น ดูรายการฟิลด์ผู้ใช้ทั้งหมดได้ในเอกสารอ้างอิง ของ API

advanced/adminSDK.gs
/**
 * Adds a new user to the domain, including only the required information. For
 * the full list of user fields, see the API's reference documentation:
 * @see https://developers.google.com/admin-sdk/directory/v1/reference/users/insert
 */
function addUser() {
  let user = {
    // TODO (developer) - Replace primaryEmail value with yours
    primaryEmail: "liz@example.com",
    name: {
      givenName: "Elizabeth",
      familyName: "Smith",
    },
    // Generate a random password string.
    password: Math.random().toString(36),
  };
  try {
    user = AdminDirectory.Users.insert(user);
    console.log("User %s created with ID %s.", user.primaryEmail, user.id);
  } catch (err) {
    // TODO (developer)- Handle exception from the API
    console.log("Failed with error %s", err.message);
  }
}

สร้างอีเมลแทน

ตัวอย่างนี้จะสร้างชื่อแทน (ชื่อเล่น) ให้กับผู้ใช้

advanced/adminSDK.gs
/**
 * Creates an alias (nickname) for a user.
 * @see https://developers.google.com/admin-sdk/directory/reference/rest/v1/users.aliases/insert
 */
function createAlias() {
  // TODO (developer) - Replace userEmail value with yours
  const userEmail = "liz@example.com";
  let alias = {
    alias: "chica@example.com",
  };
  try {
    alias = AdminDirectory.Users.Aliases.insert(alias, userEmail);
    console.log("Created alias %s for user %s.", alias.alias, userEmail);
  } catch (err) {
    // TODO (developer)- Handle exception from the API
    console.log("Failed with error %s", err.message);
  }
}

แสดงรายการกลุ่มทั้งหมด

ตัวอย่างนี้จะแสดงรายการกลุ่มทั้งหมดในโดเมน

advanced/adminSDK.gs
/**
 * Lists all the groups in the domain.
 * @see https://developers.google.com/admin-sdk/directory/reference/rest/v1/groups/list
 */
function listAllGroups() {
  let pageToken;
  let page;
  do {
    page = AdminDirectory.Groups.list({
      domain: "example.com",
      maxResults: 100,
      pageToken: pageToken,
    });
    const groups = page.groups;
    if (!groups) {
      console.log("No groups found.");
      return;
    }
    // Print group name and email.
    for (const group of groups) {
      console.log("%s (%s)", group.name, group.email);
    }
    pageToken = page.nextPageToken;
  } while (pageToken);
}

เพิ่มสมาชิกกลุ่ม

ตัวอย่างนี้จะเพิ่มผู้ใช้ลงในกลุ่มที่มีอยู่ในโดเมน

advanced/adminSDK.gs
/**
 * Adds a user to an existing group in the domain.
 * @see https://developers.google.com/admin-sdk/directory/reference/rest/v1/members/insert
 */
function addGroupMember() {
  // TODO (developer) - Replace userEmail value with yours
  const userEmail = "liz@example.com";
  // TODO (developer) - Replace groupEmail value with yours
  const groupEmail = "bookclub@example.com";
  const member = {
    email: userEmail,
    role: "MEMBER",
  };
  try {
    AdminDirectory.Members.insert(member, groupEmail);
    console.log(
      "User %s added as a member of group %s.",
      userEmail,
      groupEmail,
    );
  } catch (err) {
    // TODO (developer)- Handle exception from the API
    console.log("Failed with error %s", err.message);
  }
}