1

我越来越

 Error calling GET https://www.googleapis.com/admin/directory/v1/users/email@example.com.com: (403) Not Authorized to access this resource/api

跑步时

        $client = new Google_Client();
        $client->setClientId(GOOGLEAPPS_CLIENT_ID);
        $client->setApplicationName(SITE_NAME);
        $key = file_get_contents(APPLICATION_PATH . 'googleapps-privatekey.p12');
        $assertion = new Google_AssertionCredentials(
                    GOOGLEAPPS_EMAIL_ADDRESS, // the service account name
                    array('https://www.googleapis.com/auth/admin.directory.user'), // see https://developers.google.com/admin-sdk/directory/v1/guides/authorizing
                    $key);
        $client->setAssertionCredentials($assertion);
        $service = new Google_DirectoryService($client);
        $user = $service->users->get('email@example.com');

我按照说明https://developers.google.com/admin-sdk/directory/v1/guides/prerequisites 并勾选启用 API 访问。我使用 Google API 控制台https://code.google.com/apis/console生成服务帐户密钥并使其正常工作。

https://groups.google.com/forum/#!msg/google-api-php-client/LM-mwmuZe7I/IA_K5v1R1UMJ

我使用了 Google PHP 库并按照说明https://code.google.com/p/google-api-php-client/wiki/OAuth2?hl=no#Service_Accounts尝试让服务帐户正常工作。调试他们的代码:我正在授权并按预期获得一个新的访问令牌https://developers.google.com/accounts/docs/OAuth2ServiceAccount

当我读到的所有内容都表明我已将其全部打开时,我无法弄清楚为什么我会收到“未授权访问此资源/api”消息。有任何想法吗?

4

4 回答 4

4

刚刚开始工作。您需要包含管理员的用户电子邮件,以便 oAuth 为该用户授权您。尝试

    $assertion = new Google_AssertionCredentials(
                GOOGLEAPPS_EMAIL_ADDRESS, // the service account name
                array('https://www.googleapis.com/auth/admin.directory.user'), // see https://developers.google.com/admin-sdk/directory/v1/guides/authorizing
                $key,
                'notasecret',
                'http://oauth.net/grant_type/jwt/1.0/bearer',
                'admin_user@email.com'
    );

此外,您还需要从管理控制台->安全->高级设置->身份验证->管理 OAuth 客户端访问权限为您请求的范围授权 client_id

于 2014-04-04T12:55:55.757 回答
1

如果你得到一个错误

Class 'Google_AssertionCredentials' not found

您正在使用较新的库,Google_AssertionCredentials现在是Google_Auth_AssertionCredentials.

请参阅: https ://github.com/google/google-api-php-client/blob/master/src/Google/Auth/AssertionCredentials.php

于 2015-02-24T12:13:57.610 回答
1

我遇到了类似的问题。我正在使用 .NET 库。该[DriveService][1]示例在创建提供程序时缺少一个参数:ServiceAccountUser,它似乎必须是管理员的电子邮件地址。我错过了它是管理员并且正在获得:

Not Authorized to access this resource/api [403]

一旦我将其切换到管理员帐户,它就起作用了。恐怕我不会说 PHP,但我希望这会有所帮助。

于 2013-10-11T20:08:45.727 回答
0

您是否授予服务帐户访问控制面板中给定范围的权限?请参阅Drive SDK 中的说明并根据需要替换 Admin SDK 范围。

于 2013-10-07T09:33:58.203 回答