ReferenceError:未定义“ AdminDirectory”

时间:2018-06-25 13:45:28

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

我正在使用示例脚本来创建用户,但始终出现此错误 ReferenceError:未定义“ AdminDirectory”

/**
 * 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);
}

1 个答案:

答案 0 :(得分:3)

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

  1. 在Google脚本编辑器中,转到“资源”>“高级Google Services”并启用“管理员目录服务”。

Advanced Google Services

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

Admin SDK