Google AnalyticsAPI登录需要输入错误

时间:2022-02-08 14:29:52

I've got a strange problem that I've tried to solve but I've tried a whole day and it just doesn't work, so I hope maybe someone else can help me with this.

我有一个奇怪的问题,我试图解决,但我已经尝试了一整天,它只是不起作用,所以我希望也许别人可以帮助我。

I've build a CMS and after you login you see the dashboard. On this dashboard you can click on a button to connect your account with Google - because I want my customers to be able to connect their Google Analytics account.

我已经构建了一个CMS,在您登录后,您会看到仪表板。在此信息中心上,您可以点击按钮将您的帐户与Google相关联 - 因为我希望我的客户能够连接其Google Analytics帐户。

Everything works fine, I've got tokens and the refresh token and everything seems to work. Even when I request my user profile data I get it printed on my screen as an array. But when I try to get the accounts that are available to view for the connected user i get this error:

一切正常,我有令牌和刷新令牌,一切似乎都有效。即使我请求我的用户个人资料数据,我也会将其作为数组打印在我的屏幕上。但是当我尝试获取可供连接用户查看的帐户时,我收到此错误:

Uncaught exception 'Google_Service_Exception' with message 'Error calling GET
https://www.googleapis.com/analytics/v3/management/accounts/~all/webproperties
key=xxxxxxxxxxxxxxxxxxxxxxxxxx: (401) Login Required'

I just don't understand why I get the message 'Login required'. Below you can see my script.

我只是不明白为什么我收到“需要登录”的消息。下面你可以看到我的脚本。

require_once('/Google/Client.php');
require_once('/Google/Service/Analytics.php');

// call object
$client = new Google_Client();
$client->setApplicationName("Google Analytics - CMS title");
$client->setClientId('xxxxxxxxxxxxxxxxx');
$client->setClientSecret('xxxxxxxxxxxxxxxx');
$client->setRedirectUri('xxxxxxxxxxxxxxxxxxx');
$client->setDeveloperKey('xxxxxxxxxxxxxxxxxxx');
$client->setApprovalPrompt('auto');
$client->setAccessType('offline');
$client->setScopes(array(
    'https://www.googleapis.com/auth/userinfo.profile',
    'https://www.googleapis.com/auth/analytics'
));

// decode
$token = $token; // this comes from my database and it's json encoded

// connect
$client->setAccessToken($token);

// call service
$service    =   new Google_Service_Analytics($client);

// get accounts from analytics
$accounts   = $service->management_accounts->listManagementAccounts();

When I try to var_dump $accounts I don't see anything because the error already occurred (Login required)

当我尝试var_dump $ accounts时,我没有看到任何因为错误已经发生(需要登录)

I think you are going to ask me now if my token is correct but it is because I tested it with other api calls.. it's just not working for this specific analytics request.

我想你现在会问我,如果我的令牌是正确的,但这是因为我用其他api调用测试它...它只是不适用于这个特定的分析请求。

I hope someone can help me with this.

我希望有人可以帮助我。

Thanks, Pim vd Molen

谢谢,Pim vd Molen

UPDATE: Something really strange just happened. I don't know how this is possible but the error just changed into another error:

更新:发生了一件非常奇怪的事。我不知道这是怎么可能的,但错误只是变成了另一个错误:

Uncaught exception 'Google_Service_Exception' with message 'Error calling GET 
https://www.googleapis.com/analytics/v3/management
/accounts?key=xxxxxxxxxxxxxxxxxx: (403) Access Not Configured. 
Please use Google Developers Console to activate the API for your project.

I've enabled the Google Analytics service ofcourse, but don't know which one i also need to enable.

我启用了Google Analytics服务,但不知道我还需要启用哪个服务。

2 个解决方案

#1


1  

The 401 error clearly states that the credentials are wrong. The Token might be valid for retrieving Data, but for retrieving user management information, you need a particular grant when asking for that token, please look here in the table you see the additional value for the scope parameter to set in your OAuth request.

401错误清楚地表明凭据是错误的。令牌可能对检索数据有效,但是为了检索用户管理信息,在请求该令牌时需要特定的授权,请在表格中查看您在OAuth请求中设置的scope参数的附加值。

#2


0  

not sure if you have solved this yet, but I've documented the process, with photos here: https://circlical.squarespace.com/blog/2014/7/18/getting-zend-framework-2-and-googles-api-to-play-nice

不确定你是否已经解决了这个问题,但我已经记录了这个过程,并在此处提供了照片:https://circlical.squarespace.com/blog/2014/7/18/getting-zend-framework-2-and-googles -API到播放漂亮

#1


1  

The 401 error clearly states that the credentials are wrong. The Token might be valid for retrieving Data, but for retrieving user management information, you need a particular grant when asking for that token, please look here in the table you see the additional value for the scope parameter to set in your OAuth request.

401错误清楚地表明凭据是错误的。令牌可能对检索数据有效,但是为了检索用户管理信息,在请求该令牌时需要特定的授权,请在表格中查看您在OAuth请求中设置的scope参数的附加值。

#2


0  

not sure if you have solved this yet, but I've documented the process, with photos here: https://circlical.squarespace.com/blog/2014/7/18/getting-zend-framework-2-and-googles-api-to-play-nice

不确定你是否已经解决了这个问题,但我已经记录了这个过程,并在此处提供了照片:https://circlical.squarespace.com/blog/2014/7/18/getting-zend-framework-2-and-googles -API到播放漂亮