Php 403调用Google Admin SDK时出错

Php 403调用Google Admin SDK时出错,php,google-api,google-api-php-client,google-admin-sdk,Php,Google Api,Google Api Php Client,Google Admin Sdk,我正在尝试使用AdminSDK PHP库列出我的google域中的用户。然而,当我试图列出我的用户时,我得到了403个用户。这就是我试过的 $client = new Google_Client(); $client->setApplicationName("My Application"); $credential = new Google_Auth_AssertionCredentials( $serviceAccount, array(

我正在尝试使用AdminSDK 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)上出现以下错误


我验证了服务帐户并在项目控制台中启用了API。有人能找出我做错了什么吗?请帮忙。我对此感到困惑了一段时间。

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

这个

加上admin.google.com->Security->manageapi访问的授权解决了这个问题。我为什么要授权是另一个问题

Error refreshing the OAuth2 token, message: '{
"error" : "unauthorized_client",
"error_description" : "Unauthorized client or scope in request."
}'
$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
);