Pergunta

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

}
Foi útil?

Solução

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top