简体   繁体   English

如何在 PHP 中使用 google admin sdk api 创建用户帐户。ZE1BFD762321E409CEEZ4AC0B6

[英]how to create user account with google admin sdk api in PHP from quickstart.php?

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我正在使用 Docs < LINK > 中的此示例,我唯一更改的是仅针对允许创建用户的 scope 读取 scope

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!请帮帮我!

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 解决了问题,因为现在我将源代码留给有同样困难的其他人。

<?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!有疑问,我有空!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM