Reputation: 41
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
Reputation: 1617
For those using the new interface for Apps Script, normally you would add a service in the editor itself (Files, Libraries, Services):
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
:
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:
Upvotes: 1
Reputation: 11278
You need to enable the API in 2 places.
Upvotes: 7