Google auth api只是返回“意外错误”

时间:2015-05-20 11:36:32

标签: php google-analytics google-analytics-api

        $client = new \Google_Client();
        $client->setApplicationName('AnalyticsAPI');
        $cred = new \Google_Auth_AssertionCredentials(
            '1010008554258-XXXX@developer.gserviceaccount.com',
            array('https://www.googleapis.com/auth/analytics.readonly'),
            \file_get_contents( __DIR__ . '/../google-keys/AnalyticsAPI-XXX.p12' )
        );
        echo 'test1/';
        $client->setAssertionCredentials($cred);
        echo 'test2';
        if($client->getAuth()->isAccessTokenExpired()) {
            echo 'test3/';
            $client->getAuth()->refreshTokenWithAssertion($cred);
            echo 'test4/';
        }

以上php代码吐出以下内容:

  

test1 / test2 / test3 /发生意外错误。请联系   网站管理员。

这意味着代码运行时会发生错误:

$client->getAuth()->refreshTokenWithAssertion($cred); 

我已经检查并三次检查了serviceaccount.com电子邮件以及p12密钥..但除了意外错误之外我什么也没得到。

之前有没有人打过这堵墙?我看不出代码有什么问题。

发布帖子:refreshTokenWithAssertion Permission Denied

我修改了上面的代码,它没有运行try catch,但尝试不会失败!:

if($client->getAuth()->isAccessTokenExpired()) {
    try {
        $client->getAuth()->refreshTokenWithAssertion($cred);
    } catch (Exception $e) {
        echo 'Failed:';
        print_r($e->getMessage());
    }
}

0 个答案:

没有答案
相关问题