0

I'm using this example from Docs <LINK> the only thing i changed was the read scope only for a scope that is allowed to create a user

to list

$optParams = array(
 'customer' => 'my_customer',
 'maxResults' => 10,
 'orderBy' => 'email',
);
$results = $service->users->listUsers($optParams);
if (count($results->getUsers()) == 0) {
  print "No users found.\n";
} else {
  print "Users:\n";
  foreach ($results->getUsers() as $user) {
   printf("%s (%s)\n", $user->getPrimaryEmail(),
   $user->getName()->getFullName());
}

}

i'm trying to create the user like this:

$setEmails = array("familyName"=>"Teste", "givenName"=>"Teste", "password"=>"Zr&gt;bXm3DBfjijwjd3a","primaryEmail"=>"teste@dominio.br");
$user->setEmails($setEmails);
$results = $service->users->insert($user);

$results = $service->users->listUsers($optParams);
print_r($results);

this is the error

Fatal error: Uncaught Google_Service_Exception: {
 "error": {
   "code": 400,
   "message": "Invalid Input: primary_user_email",
   "errors": [
    {
     "message": "Invalid Input: primary_user_email",
     "domain": "global",
     "reason": "invalid"
    }
   ]
 }
}

help me please!

  • Have you tried using the try this API for [users: insert](https://developers.google.com/admin-sdk/directory/reference/rest/v1/users/insert) to check if it works there? If it does, could you share the full code? – Kessy Mar 26 '21 at 14:48
  • Yes, it works perfectly in the google api. this is my code <[link](https://pastebin.com/uASBDRQs)> – Francisco Alves Mar 28 '21 at 18:10

1 Answers1

0

With a little more effort I managed to solve using cURL in PHP, since now I leave the source code to other people who have the same difficulty.

<?php

 $curl = curl_init();

 curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://admin.googleapis.com/admin/directory/v1/users? 
  access_token=YOUR_TOKEN',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS =>'{
   "name": {
    "familyName": "teste",
    "givenName": "teste"
   },
   "password": "senha@teste!12345",
   "primaryEmail": "teste@teste.com.br"
   }',
  CURLOPT_HTTPHEADER => array(
   'Content-Type: application/json'
  ),
 ));

 $response = curl_exec($curl);

 curl_close($curl);
 echo $response;

Doubts, I'm available!