【问题标题】:'Google_Exception' with message 'Cant add services after having authenticated'带有消息“经过身份验证后无法添加服务”的“Google_Exception”
【发布时间】:2012-09-06 01:01:04
【问题描述】:

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

我所有的身份验证和数据提取都可以正常工作,除了这个问题:我第一次获得新的 Google 授权代码(例如:“4/-xbSbg....”)并进行身份验证,然后尝试调用一个新的 Google_AnalyticsService() 对象,它会抛出错误:

'Google_Exception' 带有消息'Cant add services after has authenticated'

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

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

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

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

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);
}

}

【问题讨论】:

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


    【解决方案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。

    【讨论】:

    • 谢谢!出于某种原因,我认为 Google_AnalyticsService 需要首先获得授权,但当然要在您尝试使用它之前。
    猜你喜欢
    • 2021-12-03
    • 2016-12-19
    • 2021-05-26
    • 1970-01-01
    • 1970-01-01
    • 2014-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多