Google API管理SDK错误(请求的客户端未经授权)

时间:2014-09-12 17:58:33

标签: google-oauth google-admin-sdk google-directory-api

快速背景:创建一个php类,用于在Google Apps帐户下添加/删除群组中的电子邮件。我使用相同的Google帐户进行Apps管理控制台在开发人员控制台下创建项目。谷歌应用程序帐户也处于试用模式,在29天后到期(待定初始付款)。

应用详情:通过Composer https://github.com/google/google-api-php-client

使用Google API客户端

其他详细信息:开发人员控制台 - >项目 - > Admin SDK已启用,管理控制台 - >安全 - >已启用Api访问

问题:获取异常“请求的客户端未经授权。”在refreshTokenWithAssertion()调用

如果我从下面的代码中注释掉$ cred-> sub =,我会得到此异常

Error calling GET https://www.googleapis.com/admin/directory/v1/groups/{groupemail}/members/{memberemail}: (403) Not Authorized to access this resource/api

代码:

static public function test() {
    try {
        $client = new Google_Client();
        $client->setApplicationName('app-name');
        $service = new Google_Service_Directory($client);

        if (!empty(self::$serviceToken)) {
            $client->setAccessToken(self::$serviceToken);
        }

        $key = file_get_contents(APP . DS . 'Config' . DS . 'google.p12');

        $cred = new Google_Auth_AssertionCredentials(
            '...........-.............@developer.gserviceaccount.com',
            array(
                'https://www.googleapis.com/auth/directory.user',
                'https://www.googleapis.com/auth/directory.group',
                'https://www.googleapis.com/auth/directory.group.member',
            ),
            $key,
            'notasecret'
        );

        $cred->sub = 'google apps account email';

        $client->setAssertionCredentials($cred);

        if($client->getAuth()->isAccessTokenExpired()) {
            $client->getAuth()->refreshTokenWithAssertion($cred);
        }
        self::$serviceToken = $client->getAccessToken();

        $resp = $service->members->get('group email address', 'email address of group member');
    }
    catch (Exception $e) {
        echo $e->getMessage();
    }
}

1 个答案:

答案 0 :(得分:2)

在你的阵列中:

        array(
            'https://www.googleapis.com/auth/directory.user',
            'https://www.googleapis.com/auth/directory.group',
            'https://www.googleapis.com/auth/directory.group.member',
        ),

我认为授权请求应该是:

        array(
            'https://www.googleapis.com/auth/admin.directory.user',
            'https://www.googleapis.com/auth/admin.directory.group',
            'https://www.googleapis.com/auth/admin.directory.group.member',
        ),

可以找到更多信息here