【问题标题】:how to create user account with google admin sdk api in PHP from quickstart.php?如何从 quickstart.php 在 PHP 中使用 google admin sdk api 创建用户帐户?
【发布时间】:2021-06-22 19:15:13
【问题描述】:

我正在使用文档中的这个示例 LINK> 我唯一更改的是仅允许创建用户的范围的读取范围

列出

$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());
}

}

我正在尝试像这样创建用户:

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

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

这是错误

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"
    }
   ]
 }
}

请帮帮我!

【问题讨论】:

  • 您是否尝试过使用 try this API for users: insert 来检查它是否在那里工作?如果是的话,你能分享完整的代码吗?
  • 是的,它在 google api 中完美运行。这是我的代码 link>

标签: php api google-api google-admin-sdk


【解决方案1】:

通过更多的努力,我设法在 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;

有疑问,我有空!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-12
    • 1970-01-01
    • 1970-01-01
    • 2022-10-19
    • 2021-09-30
    • 2017-12-07
    • 1970-01-01
    • 2014-11-20
    相关资源
    最近更新 更多