首页 > 解决方案 > ReferenceError:未定义“AdminDirectory”

问题描述

我正在使用示例脚本来创建用户,但它不断收到此错误 ReferenceError: "AdminDirectory" is not defined

/**
 * 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() {
  var user = {
    primaryEmail: 'liz@mydomain.com',
    name: {
      givenName: 'Elizabeth',
      familyName: 'Smith',
    },
    // Generate a random password string.
    password: Math.random().toString(36),
  };
  user = AdminDirectory.Users.insert(user);
  Logger.log('User %s created with ID %s.', user.primaryEmail, user.id);
}

标签: google-apps-scriptgoogle-admin-sdk

解决方案


您需要在 2 个地方启用 API。

  1. 在 Google Script Editor 中,转到 Resources > Advanced Google Services 并启用 Admin Directory Service。

高级谷歌服务

  1. 接下来转到资源 > Google Cloud Platform 项目 > 启用 API 和服务。搜索 Admin SDK 并启用 API。

管理 SDK


推荐阅读