Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

188
Visualizações
Automate user creation using google app script in Google Admin SDK

I want to automate user creation in google as an Admin where I use app script to do it, however from the documentation I'm reading I'm not quite sure if I'm doing it right since I'm getting some errors in my code, like after POST and the Script not working.

function createUsers() {
  const userjson = {
      "primaryEmail": "atest@example.com",
  "name": {
    "givenName": "afirstName",
    "familyName": "alastName"
  },
  "suspended": false,
  "password": "pass2022",
  "hashFunction": "SHA-1",
  "changePasswordAtNextLogin": true,
  "ipWhitelisted": false,
  "orgUnitPath": "myOrgPath",
  };

  const optionalArgs = {
   customer: 'my_customer',
   orderBy: 'email'
  };

  POST https://admin.googleapis.com/admin/directory/v1/users

  try {
    const response = AdminDirectory.Users.list(optionalArgs);
    const users = response.users;

    //check if user exists
    if (!users || users.length === 0) 
    //create new user
    return AdminDirectory.newUser(userjson);

    // Print user exists
    Logger.log('User Existing');
    
  } catch (err) {
    // TODO (developer)- Handle exception from the Directory API
    Logger.log('Failed with error %s', err.message);
  }
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

As per the official documentation, if you want to do it with Google Apps Script, you should format your code as follows:

function createUsers() {
  const userInfo = {
    "primaryEmail": "jvd@domain.com",
    "name": {
      "givenName": "Jackie",
      "familyName": "VanDamme"
    },
    "suspended": false,
    "password": "thisisasupersecret",
    "changePasswordAtNextLogin": true,
    "ipWhitelisted": false
  };
  try{
    AdminDirectory.Users.insert(userInfo);
    console.log("User added");
  } catch(error){
    const {code, message} = error.details;
    if(code === 409 && message === "Entity already exists."){
      console.log("User already exists");
    } else {
      console.log(`${code} - ${message}`);
    }
  }  
}

If you have any doubts about how to use the user resource payload, please refer to the official documentation of the REST API.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda