'Google_Exception'消息'通过身份验证后无法添加服务' [英] 'Google_Exception' with message 'Cant add services after having authenticated'

查看:119
本文介绍了'Google_Exception'消息'通过身份验证后无法添加服务'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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()的第一个分支可以,但身份验证调用不能正常工作。



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



重要提示:我正在使用Google Auth for Installed Apps,所以我们要求一个来自用户的授权码,并使用它来获得授权令牌。



get_option(),set_option()& update_option()是WP本地函数,不是问题的一部分。

这是我的代码:

  class GoogleAnalyticsStats 
{
var $ client = false;

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

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

//魔术。从Analytics Service而不是关联数组返回对象。
$ this-> client-> setUseObjects(true);


函数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);

}

返回true;


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

}


解决方案

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

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

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

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

//魔术。从Analytics Service而不是关联数组返回对象。
$ this-> client-> setUseObjects(true);

$ this-> analytics =新Google_AnalyticsService($ this->客户端);
}
...

现在,您可以拨打分析来自 getSingleProfile 中的API。


I am working on a WP plugin with Google Analytics, using Oauth 2.0.

All of my authentication & data pulls work fine, with the exception of this one issue: the first time I get a new Google authorization code (ex: "4/-xbSbg...." ) & authenticate, then try to call a new Google_AnalyticsService() object, it tosses back the error:

'Google_Exception' with message 'Cant add services after having authenticated'

This is on line 109: http://code.google.com/p/google-api-php-client/source/browse/trunk/src/apiClient.php?r=258

Once I refresh the page that calls this code, it works fine - ie, the first branch of the check_login() is ok, but the authentication call is not working correctly.

You see that the code seems to be complaining because I DID authenticate first, and the message says I shouldn't do that. The comment & code really have me confused what my issue is (login code not very clean yet, I realize).

IMPORTANT NOTE: I am using the Google Auth for Installed Apps, and so we are asking for an auth code from user, and using that to obtain the auth token.

get_option(), set_option() & update_option() are WP native functions that are not a part of the problem

Here is my code:

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

}

解决方案

You will need to move $analytics = new Google_AnalyticsService($this->client); inside function GoogleAnalyticsStats(), and preferably turn $analytics into a member variable.

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

Now, you can make calls to the analytics API from within getSingleProfile.

这篇关于'Google_Exception'消息'通过身份验证后无法添加服务'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆