【问题标题】:Google Apps Admin SDK Directory API 403 in PHPPHP 中的 Google Apps Admin SDK 目录 API 403
【发布时间】:2013-10-01 09:03:08
【问题描述】:

我明白了

 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 APIs 控制台 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”消息。有什么想法吗?

【问题讨论】:

  • 嘿,史蒂夫,你最终在 PHP 中得到了这个工作吗?
  • 没有。我决定等到谷歌完成他们的控制台更新。他们的文档全都一团糟:你绕着圈子点击指向当前文档的链接,但最后却是过时的文档。我已经看到了用于设置服务帐户的三种不同用户界面。我想我只需要等到他们把它整理好。
  • gotchya,感谢您的回复。

标签: php google-api google-apps


【解决方案1】:

刚刚开始工作。您需要包含管理员的用户电子邮件,以便 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

【讨论】:

    【解决方案2】:

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

    Not Authorized to access this resource/api [403]
    

    我将它切换到管理员帐户后,它就可以工作了。恐怕我不会说 PHP,但我希望这会有所帮助。

    【讨论】:

      【解决方案3】:

      如果遇到错误

      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

      【讨论】:

        【解决方案4】:

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

        【讨论】:

        • 是的,我做到了。在旧的管理控制台中有点纠结:它一直链接到已过时的旧 OAuth 1.0 文档。仍然没有工作。可能需要等到我们的控制台升级到新版本。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-11-22
        • 1970-01-01
        • 1970-01-01
        • 2014-01-19
        • 1970-01-01
        • 1970-01-01
        • 2013-12-16
        相关资源
        最近更新 更多