【发布时间】: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