gpt4 book ai didi

PHP - Google Admin SDK - 使用服务帐户进行身份验证 "Not Authorized to access this resource/api"

转载 作者:行者123 更新时间:2023-12-02 00:50:32 30 4
gpt4 key购买 nike

我正在使用库 google/google-api-php-client 并且正在使用服务帐户创建身份验证。

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

但我返回我要求以下内容:

Google_Service_Exception in REST.php line 118:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "forbidden",
"message": "Not Authorized to access this resource/api"
}
],
"code": 403,
"message": "Not Authorized to access this resource/api"
}
}

我的功能:

$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);

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

最佳答案

我可以解决这个问题。我在域中缺少我的用户管理员设置的主题:

$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);

关于PHP - Google Admin SDK - 使用服务帐户进行身份验证 "Not Authorized to access this resource/api",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40347946/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com