我创建了一个服务帐户,并为它的客户端 ID 指定了范围https://www.googleapis.com/auth/admin.directory.group
当我运行以下代码时,我收到 403 错误:权限不足。
<?php
// Requires >= PHP 5.4
require_once(__DIR__ . '/vendor/autoload.php');
date_default_timezone_set('America/Chicago');
$settings = [
'creds_path' => '/path/to/service_creds.json',
'group_email' => 'group@email.com',
'service_email' => 'service@email.com'
];
putenv("GOOGLE_APPLICATION_CREDENTIALS={$settings['creds_path']}");
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->addScope(Google_Service_Directory::ADMIN_DIRECTORY_GROUP);
// $client->setSubject('admin@email.com');
$service = new Google_Service_Groupssettings($client);
try {
print_r($service->groups->get($settings['group_email'], ['alt' => 'json']));
} catch(Google_Service_Exception $e) {
if($e->getCode() == 404) {
echo "Group {$settings['group_email']} not found.\n";
exit;
} elseif($e->getCode() == 403) {
echo "Insufficient Permissions.\n";
exit;
} else {
throw $e;
}
}
我在某处读到服务帐户必须模拟有权访问 admin sdk 的人,所以这就是注释掉的行所尝试的,但它没有用。
有谁知道出了什么问题?
正在使用的代码required
来自https://github.com/google/google-api-php-client