PHP - Google Admin SDK - 使用服务帐户进行身份验证“未授权访问此资源/ api”

时间:2016-10-31 18:06:40

标签: php google-admin-sdk

我正在使用图书馆google / google-api-php-client,我正在使用服务帐户创建身份验证。

我已在https://console.developers.google.com/创建了服务帐户,并将域范围的权限添加到了我的服务帐户。

但是我回来请求以下内容:

$client = new \Google_Client();

$credentials_file = base_path() . '/service-account.json';

if ($credentials_file = $this->checkServiceAccountCredentialsFile($credentials_file)) {
    $client->setAuthConfig($credentials_file);
}

$client->setApplicationName("app-name");
$client->setScopes([
    'https://www.googleapis.com/auth/admin.directory.user',
    'https://www.googleapis.com/auth/admin.directory.user.readonly'
]);

$service = new \Google_Service_Directory($client);

$userKey = 'user@domain.com';

$results = $service->users->get($userKey);

var_dump($results);

我的功能:

qstat -u '*' | grep 123456

如果我在这里尝试工作。 https://developers.google.com/admin-sdk/directory/v1/reference/users/get#auth

1 个答案:

答案 0 :(得分:1)

我可以解决问题。我在域名上缺少用户管理员设置主题:

$client->setSubject("admin@yourdomain.com");

结果:

$client = new \Google_Client();

$credentials_file = base_path() . '/service-account.json';

if ($credentials_file = $this->checkServiceAccountCredentialsFile($credentials_file)) {
    $client->setAuthConfig($credentials_file);
}

$client->setApplicationName("app-name");
$client->setSubject("admin@yourdomain.com");
$client->setScopes([
    'https://www.googleapis.com/auth/admin.directory.user',
    'https://www.googleapis.com/auth/admin.directory.user.readonly'
]);

$service = new \Google_Service_Directory($client);

$userKey = 'user@domain.com';

$results = $service->users->get($userKey);

var_dump($results);