“Google_Exception”,其中包含“经过身份验证后无法添加服务”的消息

时间:2012-09-13 00:23:15

标签: google-analytics oauth-2.0 google-authentication

我正在使用Oauth 2.0开发使用Google Analytics的WP插件。

我的所有身份验证和除了这一个问题之外,数据工作得很好:我第一次获得新的Google授权码(例如:“4 / -xbSbg ....”)&验证,然后尝试调用新的Google_AnalyticsService()对象,它会抛出错误:

'Google_Exception',消息'在经过身份验证后无法添加服务'

这是第109行:http://code.google.com/p/google-api-php-client/source/browse/trunk/src/apiClient.php?r=258

一旦刷新调用此代码的页面,它就可以正常工作 - 即check_login()的第一个分支正常,但验证调用无法正常工作。

您看到代码似乎在抱怨,因为我首先进行DID身份验证,并且消息显示我不应该这样做。评论&代码真让我困惑我的问题是什么(登录代码还不是很干净,我意识到)。

重要说明:我正在使用Google Auth for Installed Apps,因此我们要求用户提供auth代码,并使用该代码获取身份验证令牌。

get_option(),set_option()& update_option()是WP本机函数,它们不是问题的一部分

这是我的代码:

class GoogleAnalyticsStats
{
var $client = false;

function GoogleAnalyticsStats()
{       
$this->client = new Google_Client();

$this->client->setClientId(GOOGLE_ANALYTICATOR_CLIENTID);
$this->client->setClientSecret(GOOGLE_ANALYTICATOR_CLIENTSECRET);
$this->client->setRedirectUri(GOOGLE_ANALYTICATOR_REDIRECT);  
$this->client->setScopes(array(GOOGLE_ANALYTICATOR_SCOPE));

// Magic. Returns objects from the Analytics Service instead of associative arrays.
$this->client->setUseObjects(true);
}

function checkLogin()
{
$ga_google_authtoken  = get_option('ga_google_authtoken');
if (!empty($ga_google_authtoken)) 
{
        $this->client->setAccessToken($ga_google_authtoken);
}
else
{
    $authCode = get_option('ga_google_token');

    if (empty($authCode)) return false;

    $accessToken = $this->client->authenticate($authCode);
    $this->client->setAccessToken($accessToken);
    update_option('ga_google_authtoken', $accessToken);         

}   

return true;
 }

function getSingleProfile()
{
$analytics = new Google_AnalyticsService($this->client);
}

}

1 个答案:

答案 0 :(得分:1)

您需要在$analytics = new Google_AnalyticsService($this->client);内移动function GoogleAnalyticsStats(),最好将$ analytics转换为成员变量。

class GoogleAnalyticsStats
{
  var $client = false;
  var $analytics = false;

  function GoogleAnalyticsStats()
  {       
    $this->client = new Google_Client();

    $this->client->setClientId(GOOGLE_ANALYTICATOR_CLIENTID);
    $this->client->setClientSecret(GOOGLE_ANALYTICATOR_CLIENTSECRET);
    $this->client->setRedirectUri(GOOGLE_ANALYTICATOR_REDIRECT);  
    $this->client->setScopes(array(GOOGLE_ANALYTICATOR_SCOPE));

    // Magic. Returns objects from the Analytics Service instead of associative arrays.
    $this->client->setUseObjects(true);

    $this->analytics = new Google_AnalyticsService($this->client);
  }
  ...

现在,您可以在getSingleProfile内调用分析API。

相关问题