gpt4 book ai didi

google-analytics - 消息为 'Cant add services after having authenticated' 的“Google_Exception”

转载 作者:行者123 更新时间:2023-12-03 15:39:26 25 4
gpt4 key购买 nike

我正在使用 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 身份验证,因此我们要求用户提供授权代码,并使用它来获取授权 token 。

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

}

最佳答案

您需要将 $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-analytics - 消息为 'Cant add services after having authenticated' 的“Google_Exception”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12397815/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com