Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
}
like image 277
Frank Avatar asked Dec 20 '25 12:12

Frank


2 Answers

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

like image 50
Amit Agarwal Avatar answered Dec 22 '25 05:12

Amit Agarwal


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

like image 36
Matthew Avatar answered Dec 22 '25 07:12

Matthew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!