我已按照https://developers.google.com/api-client-library/php/auth/service-accounts
的指示进行操作我已在
的管理控制台中添加了我的服务帐户的客户端ID的范围https://www.googleapis.com/auth/admin.directory.group
https://www.googleapis.com/auth/admin.directory.user
https://www.googleapis.com/auth/admin.directory.customer
https://www.googleapis.com/auth/admin.directory.domain
以下是我试图运行的代码
putenv('GOOGLE_APPLICATION_CREDENTIALS=JSONFILELOCATION');
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->setApplicationName("Directory");
$client->setScopes(array(
Google_Service_Directory::ADMIN_DIRECTORY_CUSTOMER,
Google_Service_Directory::ADMIN_DIRECTORY_USER
));
$client->setSubject("directory@directory-152416.iam.gserviceaccount.com");
//$client->setSubject(SUPERADMINEMAILADDRESS);
$service = new Google_Service_Directory($client);
// Print the first 10 users in the domain.
$optParams = array(
'domain'=>'MYDOMAIN',
'maxResults' => 10,
'orderBy' => 'email',
);
$results = $service->users->listUsers($optParams);
我得到的结果是未经授权的客户
Fatal error: Uncaught exception 'Google_Service_Exception' with message '{
"error": "unauthorized_client",
"error_description": "Unauthorized client or scope in request."
}
' in /var/web/composer/googlephpapi/vendor/google/apiclient/src/Google/Http/REST.php:118
Stack trace:
#0 /var/web/composer/googlephpapi/vendor/google/apiclient/src/Google/Http/REST.php(94): Google_Http_REST::decodeHttpResponse(Object(GuzzleHttp\Psr7\Response), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...')
#1 [internal function]: Google_Http_REST::doExecute(Object(GuzzleHttp\Client), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...')
#2 /var/web/composer/googlephpapi/vendor/google/apiclient/src/Google/Task/Runner.php(181): call_user_func_array(Array, Array)
#3 /var/web/composer/googlephpapi/vendor/google/apiclient/src/Google/Http/REST.php(58): Google_Task_Runner->run()
#4 /var/web/composer/googlephpapi/vendor/google/apiclient/src/Google/Client.php(781): Google_Http_REST::execute(Object(GuzzleHttp\Client), Object(GuzzleHttp\Psr7\Request), in /var/web/composer/googlephpapi/vendor/google/apiclient/src/Google/Http/REST.php on line 118
我尝试使用服务帐户电子邮件地址。但是我得到了许可错误。如果我使用超级管理员的电子邮件地址,我会得到很好的结果。我是否缺少能够使用服务帐户的设置?我认为进行域范围授权将允许我使用服务帐户进行完全访问而无需使用真实用户。
答案 0 :(得分:0)
这是另一个尝试的例子。
pullquote