2012-09-13 31 views
3

我正在使用Oauth 2.0与Google Analytics一起使用WP插件。'Google_Exception'消息'经过身份验证后无法添加服务'

我所有的认证数据&再换做工精细,与此一个问题的例外:我第一次得到一个新的谷歌授权码(例如:“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()的第一个分支是可以的,但验证呼叫不能正常工作。

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

重要提示:我正在使用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); 
} 

} 

回答

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。

+0

谢谢!有些理由让我头脑中的Google_AnalyticsService需要首先得到授权,但当然,直到您尝试使用它。 – jmadsen

相关问题