Google Admin SDK 出现 403 权限不足错误

Google Admin SDK getting 403 Insufficient Permissions error

我创建了一个服务帐户并为其客户端 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

结果是我用错了class。我将 Google_Service_Groupssettings 切换为 Google_Service_Directory 并取消注释 setSubject 调用,现在它可以工作了。