Google PHP客户端API-错误400管理员SDK 问题:解决方案:参考:


我正在尝试让用户与Google GSuite for Education关联的组。 我已经开始按照本教程列出与该关联相关的所有用户,但是我得到了:错误400-“ message”:“无效的输入。
到目前为止,我已经创建了一个带有服务帐户的项目,因为我需要没有用户交互,因为必须自动完成获取。 之后,我将该帐户设置为启用了G Suite域范围授权,并将其放入具有https://www.googleapis.com/auth/admin.directory.group.readonlyhttps://www.googleapis.com/auth/admin.directory.user.readonly域的授权API客户端中,但是当我运行代码时,出现上面的错误
这是代码:

<?php
  require_once './google-api/vendor/autoload.php';
  putenv('GOOGLE_APPLICATION_CREDENTIALS='.__DIR__.'/Culturalia.json');

  function getGoogleclient() {
    return getServiceaccountClient();
  }

  function getServiceaccountClient() {
    try {
        // Create and configure a new client object.
        $client = new Google_Client();
        $client->useApplicationDefaultCredentials();
        $client->addScope([Google_Service_Directory::ADMIN_DIRECTORY_USER_READONLY]);
        return $client;
    } catch (Exception $e) {
        print "An error occurred: " . $e->getMessage();
    }
  }

  $client = getGoogleclient();
  $service = new Google_Service_Directory($client);


  // Print the first 10 users in the domain.
  $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());
    }
  }
?>

但是我想这很可能是我在某处缺少的授权,我们将不胜感激。

lx58471571 回答:Google PHP客户端API-错误400管理员SDK 问题:解决方案:参考:

问题:

您可能已向服务帐户授予域范围的委派(使用this guide再次检查),但是您尚未使用此委派模拟域中的任何帐户。那就是你所缺少的部分。

授予域范围的委派目的是使服务帐户能够代表您域中的任何帐户访问资源。但是,您需要指定要模拟的帐户;否则,您不会冒充任何人,并且服务帐户的行为就好像您根本没有授予域范围的授权一样。

由于从技术上讲该服务帐户不属于域,因此在尝试列出域用户时会出现错误。

解决方案:

要模拟帐户,您需要在设置client时指定其电子邮件地址,如下所示:

$user = "email-address@your-domain";
$client->setSubject($user);

参考:

本文链接:https://www.f2er.com/2387225.html

大家都在问