Frank
Frank

Reputation: 41

ReferenceError: "AdminDirectory" is not defined

I am using a sample script to create a user but it keeps get this error 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: '[email protected]',
    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);
}

Upvotes: 3

Views: 7601

Answers (2)

Matthew
Matthew

Reputation: 1617

For those using the new interface for Apps Script, normally you would add a service in the editor itself (Files, Libraries, Services):

Add a service in the editor interface of Google Apps

Unfortunately the AdminDirectory service does not seem to be available in the new interface (as at July 2022). For the next few months while the classic editor is still available, you can click "Use classic editor".

From there it's as Amit explained, Resources menu → Advanced Google services... then turn on AdminDirectory next to Admin SDK API:
Advanced Google services... enter image description here

Once you've added the service you can go back to the new interface to continue editing and it will show up in the expected spot:
AdminDirectory service in new interface

Upvotes: 1

Amit Agarwal
Amit Agarwal

Reputation: 11278

You need to enable the API in 2 places.

  1. Inside the Google Script Editor, go to Resources > Advanced Google Services and enable the Admin Directory Service.

Advanced Google Services

  1. Next go to Resources > Google Cloud Platform Project > Enable API and Services. Search for Admin SDK and enable the API.

Admin SDK

Upvotes: 7

Related Questions