Google API使用服务帐户模拟已连接的电子邮件时未经授权的客户端错误

时间:2015-08-06 13:45:59

标签: google-analytics google-api google-oauth

我一直在使用Google api客户端使用下面的方法访问我公司过去3个月的分析数据,并且运行正常。

然后,我不得不使用不同的分析帐户重复此过程,除了{ "error" : "unauthorized_client", "error_description" : "Unauthorized client or scope in request." }

之外我无法获得任何其他内容

该帐户在我想要模拟的帐户下的console.developers.google.com上设置,并且正确复制了详细信息/ .p12键。

我不在谷歌应用领域,因此不需要授权。我无法理解为什么一个服务帐户/模拟工作正常,但我无法重新创建任何其他分析帐户。

以下是我正在使用的内容:

function __construct () 
{
    parent::__construct();

    // accessed via google developer console
    $client_email = '****.gserviceaccount.com';
    $private_key = file_get_contents(__DIR__ .'/cm-suite.p12');
    $scopes = array(\Google_Service_Analytics::ANALYTICS_READONLY);
    $user_to_impersonate = '****EMAIL***';

    $credentials = new \Google_Auth_AssertionCredentials(
        $client_email,
        $scopes,
        $private_key,
        'notasecret',                                 
        'http://oauth.net/grant_type/jwt/1.0/bearer', 
        $user_to_impersonate
    );


    $client = new \Google_Client();
    $client->setAssertionCredentials($credentials);

    if ($client->getAuth()->isAccessTokenExpired()) 
        $client->getAuth()->refreshTokenWithAssertion();

    // set reference for instance methods
    $this->client = &$client;
}   

帐户已在Google Analytics中正确添加,并且所有安全详细信息都是正确的(如果我不尝试使用子帐户,则服务帐户可以正确连接)。

这个问题是编程语言无关的我认为我的例子在使用不同的帐户之前是有效的 - 所以必须是配置问题,或者我错过设置的事情。

我无法理解为什么它适用于一个谷歌帐户,而不是另一个。

0 个答案:

没有答案