Admin SDK Directory 服务

Admin SDK Directory 服务可让您在 Apps 脚本中使用 Admin SDK 的 Directory API。此 API 使网域(包括转销商)的管理员能够管理其网域中的设备、群组、用户和其他实体。 Google Workspace

参考

如需详细了解此服务,请参阅 Admin SDK Directory API 的参考文档。与 Apps 脚本中的所有高级服务一样,Admin SDK Directory 服务使用与公共 API 相同的对象、方法和参数。如需了解详情,请参阅如何确定方法签名

如需报告问题并寻求其他支持,请参阅 Admin SDK 目录支持指南

示例代码

下面的示例代码使用了该 API 的版本 1

列出所有用户

此示例列出了网域中所有用户的名字。

高级/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) {
      Logger.log('No users found.');
      return;
    }
    // Print the user's full name and email.
    for (const user of users) {
      Logger.log('%s (%s)', user.name.fullName, user.primaryEmail);
    }
    pageToken = page.nextPageToken;
  } while (pageToken);
}

获取用户

此示例会按电子邮件地址获取用户,并将所有数据记录为 JSON 字符串。

高级/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);
    Logger.log('User data:\n %s', JSON.stringify(user, null, 2));
  } catch (err) {
    // TODO (developer)- Handle exception from the API
    Logger.log('Failed with error %s', err.message);
  }
}

添加用户

此示例将向网域中添加新用户,其中仅包含所需信息。如需查看用户字段的完整列表,请参阅 API 的参考文档

高级/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);
    Logger.log('User %s created with ID %s.', user.primaryEmail, user.id);
  } catch (err) {
    // TODO (developer)- Handle exception from the API
    Logger.log('Failed with error %s', err.message);
  }
}

创建别名

此示例为用户创建了别名(昵称)。

高级/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);
    Logger.log('Created alias %s for user %s.', alias.alias, userEmail);
  } catch (err) {
    // TODO (developer)- Handle exception from the API
    Logger.log('Failed with error %s', err.message);
  }
}

列出所有群组

此示例列出了网域中的所有群组。

高级/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) {
      Logger.log('No groups found.');
      return;
    }
    // Print group name and email.
    for (const group of groups) {
      Logger.log('%s (%s)', group.name, group.email);
    }
    pageToken = page.nextPageToken;
  } while (pageToken);
}

添加群组成员

此示例将用户添加到网域中的现有群组。

高级/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);
    Logger.log('User %s added as a member of group %s.', userEmail, groupEmail);
  } catch (err) {
    // TODO (developer)- Handle exception from the API
    Logger.log('Failed with error %s', err.message);
  }
}