【问题标题】:403 Error in calling Google Admin SDK调用 Google Admin SDK 时出现 403 错误
【发布时间】:2015-09-01 22:35:00
【问题描述】:

我正在尝试使用 Admin SDK PHP 库列出我的 google 域中的用户。但是,当我尝试列出我的用户时,我得到了 403 个用户。这是我试过的

$client = new Google_Client();
$client->setApplicationName("My Application");
$credential = new Google_Auth_AssertionCredentials(
            $serviceAccount,
            array('https://www.googleapis.com/auth/admin.directory.user'),
            $privateKey,
            'notasecret',
            'http://oauth.net/grant_type/jwt/1.0/bearer'
            );

$client->setAssertionCredentials($credential);
if($client->getAuth()->isAccessTokenExpired())
{
    $client->getAuth()->refreshTokenWithAssertion($credential);
}

$service = new Google_Service_Directory($client);

$optParams = array('domain' => 'mydomain');
$results = $service->users->listUsers($optParams);

但是我收到了这个 403 错误

Error calling GET https://www.googleapis.com/admin/directory/v1/users?domain=mydomain: (403) Not Authorized to access this resource/api

正如其他类似帖子中所建议的那样,我也尝试包括如下所示的委派管理员

$credential                = new Google_Auth_AssertionCredentials(
            $serviceAccount,,
            array('https://www.googleapis.com/auth/admin.directory.user'),
            $privateKey,
            'notasecret',
            'http://oauth.net/grant_type/jwt/1.0/bearer',
             'admin@mydomain.com'
    );

但这在 refreshTokenWithAssertion($credential) 上给出了以下错误

Error refreshing the OAuth2 token, message: '{
"error" : "unauthorized_client",
"error_description" : "Unauthorized client or scope in request."
}'

我验证了服务帐户并在项目的控制台中启用了 API。谁能弄清楚我做错了什么?请帮忙。我在这方面有一段时间了。

【问题讨论】:

    标签: php google-api google-api-php-client google-admin-sdk


    【解决方案1】:

    我找到了解决此错误的方法。我在 Google_Auth_AssertionCredentials 中添加了“子”,如下所示,并在 admin.google.com->Security->Manage API access 中添加了客户端 ID 和范围并对其进行了授权。

    这个

    $credential                = new Google_Auth_AssertionCredentials(
            $serviceAccount,,
            array('https://www.googleapis.com/auth/admin.directory.user'),
            $privateKey,
            'notasecret',
            'http://oauth.net/grant_type/jwt/1.0/bearer',
             'admin@mydomain.com', false
    );
    

    加上在 admin.google.com->Security->Manage API 访问权限的授权解决了这个问题。为什么我必须授权是另一个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-11
      • 2016-10-27
      • 1970-01-01
      • 2017-07-03
      • 1970-01-01
      • 2018-04-22
      • 1970-01-01
      • 2013-07-04
      相关资源
      最近更新 更多