【发布时间】:2020-08-05 00:15:31
【问题描述】:
我正在尝试在 google GSuite for Education 上获取与用户关联的组。
我已经开始按照教程列出与关联相关的所有用户,但我得到:错误 400 -“消息”:“无效输入。
到目前为止,我已经创建了一个带有服务帐户的项目,因为我需要没有用户交互,因为必须自动完成获取。
之后,我将帐户设置为启用 G Suite 域范围委派,并将其放在具有 https://www.googleapis.com/auth/admin.directory.group.readonly 和 https://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());
}
}
?>
但我猜这更有可能是我在某处丢失的授权,任何帮助将不胜感激。
【问题讨论】:
标签: php google-authentication google-admin-sdk google-client