“权限不足”谷歌分析API服务账号

时间:2022-03-03 15:17:30

I'm trying to fetch data with the Google Analytics API through service accounts.

我尝试通过服务帐户获取谷歌分析API的数据。

I have been searching on * how to do this and been using the exact same code found on several posts but getting problems to get it working.

我一直在*上搜索如何做到这一点,并使用了几个帖子中找到的完全相同的代码,但在让它工作时遇到了问题。

Sources:

来源:

Service Applications and Google Analytics API V3: Server-to-server OAuth2 authentication?

服务应用程序和谷歌分析API V3:服务器对服务器OAuth2身份验证?

Service Applications and Google Analytics API V3: Error 101 (net::ERR_CONNECTION_RESET)

服务应用程序和谷歌分析API V3: Error 101 (net::ERR_CONNECTION_RESET)

http://code.google.com/p/google-api-php-client/source/browse/trunk/examples/prediction/serviceAccount.php

http://code.google.com/p/google-api-php-client/source/browse/trunk/examples/prediction/serviceAccount.php

This is what i got:

这就是我得到的:

require_once('googleAPIGoogle_Client.php');
require_once('googleAPI/contrib/Google_AnalyticsService.php');

const CLIENT_ID = 'xxxxxxxx001.apps.googleusercontent.com';
const SERVICE_ACCOUNT_NAME = 'xxxxxx001@developer.gserviceaccount.com';


$keyfile = $_SERVER['DOCUMENT_ROOT']."/xxxxxxx284-privatekey.p12";


$client = new Google_Client();
$client->setAccessType('offline');
$client->setApplicationName("cc insights");


$key = file_get_contents($keyfile);
$client->setClientId(CLIENT_ID);

$client->setAssertionCredentials(new Google_AssertionCredentials(
    SERVICE_ACCOUNT_NAME,
    array('https://www.googleapis.com/auth/analytics.readonly'),
    $key)
);


$service = new Google_AnalyticsService($client);

$data = $service->data_ga->get("ga:xxxx7777", "2012-01-01", "2013-01-25", "ga:pageviews");
var_dump($data);

when executing i got the following error:

在执行时,我有以下错误:

Fatal error: Uncaught exception 'Google_ServiceException' with message 'Error calling GET https://www.googleapis.com/analytics/v3/data/ga?ids=ga%3Axxxx7777&start-date=2012-01-01&end-date=2013-01-25&metrics=ga%3Apageviews: (403) User does not have sufficient permissions for this profile.' in /Applications/XAMPP/xamppfiles/htdocs/insights/application/libraries/googleAPI/io/Google_REST.php:66 Stack trace: #0 /Applications/XAMPP/xamppfiles/htdocs/insights/application/libraries/googleAPI/io/Google_REST.php(36): Google_REST::decodeHttpResponse(Object(Google_HttpRequest)) #1 /Applications/XAMPP/xamppfiles/htdocs/insights/application/libraries/googleAPI/service/Google_ServiceResource.php(178): Google_REST::execute(Object(Google_HttpRequest)) #2 /Applications/XAMPP/xamppfiles/htdocs/insights/application/libraries/googleAPI/contrib/Google_AnalyticsService.php(383): Google_ServiceResource->__call('get', Array) #3 /Applications/XAMPP/xamppfiles/htdocs/insights/application/libraries/Google.php(46): Google_DataGaServiceResource->get('g in /Applications/XAMPP/xamppfiles/htdocs/insights/application/libraries/googleAPI/io/Google_REST.php on line 66

致命错误:未捕获的异常“Google_ServiceException”和消息“错误调用GET https://www.googleapis.com/analytics/v3/data/ga?id =ga% 3axxxx77and start-date=2012-01-01&end-date=2013-01-25&metrics=ga%3Apageviews:(403)用户对该概要没有足够的权限。在/应用程序/ XAMPP / xamppfiles /根/观点/应用程序/图书馆/ googleAPI / io / Google_REST。php /应用/XAMPP/xamppfiles/htdocs/insights/应用/库/googleAPI/io/Google_REST.php(36): Google_REST: decodeHttpResponse(Object(Google_HttpRequestGoogle_ServiceResource->__call('get',数组)#3 /Applications/XAMPP/xamppfiles/htdocs/insights/application/ library /Google.php(46):php在第66行

Because i'm using the exact same code as working examples, i think it might be a problem with the API or service account settings?

因为我使用的代码与工作示例完全相同,我认为这可能是API或服务帐户设置的问题?

This is the user that i added to the analytics account : “权限不足”谷歌分析API服务账号

这是我添加到分析账户的用户:

and this is api access accounts: “权限不足”谷歌分析API服务账号

这是api访问帐户:

Does anybody know what i'm doing wrong?

有人知道我做错了什么吗?

11 个解决方案

#1


69  

I was getting exactly the same error and this solved it for me:

我得到了完全相同的错误,这就帮我解决了:

I was using the account ID instead of the View ID. Switching to the View ID fixed it - it’s always the “View ID” and not the account or property ID (which looks like “UA-xxx”). The View ID is for a specific web property and can be found in the Admin->View (3rd column)->View Settings. It’s an integer with no dashes.

我使用的是帐户ID而不是视图ID。切换到视图ID时,它总是“视图ID”,而不是帐户或属性ID(看起来像“ui -xxx”)。视图ID用于特定的web属性,可以在Admin->视图(第三列)->视图设置中找到。它是一个没有破折号的整数。

https://*.com/a/15789266/1391229

https://*.com/a/15789266/1391229

#2


10  

Had the same issue today, the profileId is hard to find, you can have it in every Url in analytics let's say https://www.google.com/analytics/web/#management/Settings/aXXXXXXwYYYYYYYpZZZZZZZ/ it's the ZZZZZZZ part

今天也有同样的问题,profileId很难找到,你可以在分析的每个Url中找到它比如https://www.google.com/analytics/web/#management/Settings/ axxxxwyyyyyyyypzzzz /这是zzzzzz部分

#3


5  

Same issue. Solved it by using the VIEW id instead of the account Id (UA-XXXXX-1).

相同的问题。通过使用视图id而不是帐户id (ui - xxxx -1)来解决这个问题。

Analytics Console > Admin -> View (Profile) -> View Settings -> View ID

AND

Make sure that you add the service account to your list of users in the Google Analytics Console. Simply setting it up in the Credentials, API and Permissions section of the developer console will still not grant it access to your analytics.

确保将服务帐户添加到谷歌分析控制台的用户列表中。仅仅在developer console的凭据、API和权限部分设置它仍然不会授予它对您的分析的访问权限。

Analytics Console > Admin -> Account -> User Management -> "Add permissions for:"

#4


4  

Got the same error but after a while I recognized that i was using this script with my google chrome (my own email adress) but the Analytics API is running on the companies email. So the solution could be to edit the settings of your google chrome. Just disconnect from the gmail account in your google chrome (settings) and test it again.

我也犯了同样的错误,但过了一会儿,我意识到我正在使用谷歌chrome(我自己的电子邮件地址),但是分析API正在公司的电子邮件上运行。所以解决方案可以是编辑你的谷歌chrome的设置。只要断开谷歌chrome(设置)中的gmail帐户,然后再次测试。

#5


4  

Use View ID Not account ID, on Admin tab, 'View Settings' 'View ID'.

使用视图ID而不是帐户ID,在Admin选项卡上,'View Settings' View ID'。

#6


3  

Helped me a lot after 2 weeks of trying to find solution:

经过两周的努力,我终于找到了解决方案:

Analytics Console -> Admin -> View (Profile) -> View Settings -> View ID

分析控制台-> Admin ->视图(概要)->视图设置->视图ID

#7


1  

On a different application I was getting "Not sufficient permissions" and found my .htaccess file had some file restrictions that caused the problem.

在另一个应用程序上,我得到了“不充分的权限”,并发现我的.htaccess文件有一些文件限制,导致了问题。

#8


1  

I had the same problem using .net library, after some tinkering, i found the solution:

我在使用。net库时遇到了同样的问题,经过一番修补之后,我找到了解决方案:

Go to your google analytics management site & add the service account as a user & grant that account the necessary rights

进入您的谷歌分析管理站点,添加服务帐户作为用户并授予该帐户必要的权利

#9


0  

might be you are using other Client ID: Client secret: to get another profile id details

可能是您正在使用其他客户机ID: Client secret:获取另一个配置文件ID详细信息

#10


0  

Had the problem.. it turns out the Account ID is hidden in the URL of your analytics account!

有问题. .原来账户ID隐藏在你的分析账户的URL里!

https://developers.google.com/analytics/devguides/reporting/core/v3/#user_reports

https://developers.google.com/analytics/devguides/reporting/core/v3/ user_reports

The URL typically looks like so:

URL通常是这样的:

https://www.google.com/analytics/web/?hl=en#management/Settings/a51343283w84330433p87396224/%3Fm.page%3DAccountSettings/

https://www.google.com/analytics/web/?hl=en管理/设置/ a51343283w84330433p87396224 / % 3 fm.page % 3 daccountsettings /

The ID we are looking for follows the letter p.

我们要找的是字母p。

In the example above, the ID that will work is: 87396224

在上面的示例中,将工作的ID是:87396224

#11


0  

Please create shared segment...It will work

请创建共享段…它将工作

http://www.periscopix.co.uk/blog/google-analytics-new-feature-shared-segments/

http://www.periscopix.co.uk/blog/google-analytics-new-feature-shared-segments/

#1


69  

I was getting exactly the same error and this solved it for me:

我得到了完全相同的错误,这就帮我解决了:

I was using the account ID instead of the View ID. Switching to the View ID fixed it - it’s always the “View ID” and not the account or property ID (which looks like “UA-xxx”). The View ID is for a specific web property and can be found in the Admin->View (3rd column)->View Settings. It’s an integer with no dashes.

我使用的是帐户ID而不是视图ID。切换到视图ID时,它总是“视图ID”,而不是帐户或属性ID(看起来像“ui -xxx”)。视图ID用于特定的web属性,可以在Admin->视图(第三列)->视图设置中找到。它是一个没有破折号的整数。

https://*.com/a/15789266/1391229

https://*.com/a/15789266/1391229

#2


10  

Had the same issue today, the profileId is hard to find, you can have it in every Url in analytics let's say https://www.google.com/analytics/web/#management/Settings/aXXXXXXwYYYYYYYpZZZZZZZ/ it's the ZZZZZZZ part

今天也有同样的问题,profileId很难找到,你可以在分析的每个Url中找到它比如https://www.google.com/analytics/web/#management/Settings/ axxxxwyyyyyyyypzzzz /这是zzzzzz部分

#3


5  

Same issue. Solved it by using the VIEW id instead of the account Id (UA-XXXXX-1).

相同的问题。通过使用视图id而不是帐户id (ui - xxxx -1)来解决这个问题。

Analytics Console > Admin -> View (Profile) -> View Settings -> View ID

AND

Make sure that you add the service account to your list of users in the Google Analytics Console. Simply setting it up in the Credentials, API and Permissions section of the developer console will still not grant it access to your analytics.

确保将服务帐户添加到谷歌分析控制台的用户列表中。仅仅在developer console的凭据、API和权限部分设置它仍然不会授予它对您的分析的访问权限。

Analytics Console > Admin -> Account -> User Management -> "Add permissions for:"

#4


4  

Got the same error but after a while I recognized that i was using this script with my google chrome (my own email adress) but the Analytics API is running on the companies email. So the solution could be to edit the settings of your google chrome. Just disconnect from the gmail account in your google chrome (settings) and test it again.

我也犯了同样的错误,但过了一会儿,我意识到我正在使用谷歌chrome(我自己的电子邮件地址),但是分析API正在公司的电子邮件上运行。所以解决方案可以是编辑你的谷歌chrome的设置。只要断开谷歌chrome(设置)中的gmail帐户,然后再次测试。

#5


4  

Use View ID Not account ID, on Admin tab, 'View Settings' 'View ID'.

使用视图ID而不是帐户ID,在Admin选项卡上,'View Settings' View ID'。

#6


3  

Helped me a lot after 2 weeks of trying to find solution:

经过两周的努力,我终于找到了解决方案:

Analytics Console -> Admin -> View (Profile) -> View Settings -> View ID

分析控制台-> Admin ->视图(概要)->视图设置->视图ID

#7


1  

On a different application I was getting "Not sufficient permissions" and found my .htaccess file had some file restrictions that caused the problem.

在另一个应用程序上,我得到了“不充分的权限”,并发现我的.htaccess文件有一些文件限制,导致了问题。

#8


1  

I had the same problem using .net library, after some tinkering, i found the solution:

我在使用。net库时遇到了同样的问题,经过一番修补之后,我找到了解决方案:

Go to your google analytics management site & add the service account as a user & grant that account the necessary rights

进入您的谷歌分析管理站点,添加服务帐户作为用户并授予该帐户必要的权利

#9


0  

might be you are using other Client ID: Client secret: to get another profile id details

可能是您正在使用其他客户机ID: Client secret:获取另一个配置文件ID详细信息

#10


0  

Had the problem.. it turns out the Account ID is hidden in the URL of your analytics account!

有问题. .原来账户ID隐藏在你的分析账户的URL里!

https://developers.google.com/analytics/devguides/reporting/core/v3/#user_reports

https://developers.google.com/analytics/devguides/reporting/core/v3/ user_reports

The URL typically looks like so:

URL通常是这样的:

https://www.google.com/analytics/web/?hl=en#management/Settings/a51343283w84330433p87396224/%3Fm.page%3DAccountSettings/

https://www.google.com/analytics/web/?hl=en管理/设置/ a51343283w84330433p87396224 / % 3 fm.page % 3 daccountsettings /

The ID we are looking for follows the letter p.

我们要找的是字母p。

In the example above, the ID that will work is: 87396224

在上面的示例中,将工作的ID是:87396224

#11


0  

Please create shared segment...It will work

请创建共享段…它将工作

http://www.periscopix.co.uk/blog/google-analytics-new-feature-shared-segments/

http://www.periscopix.co.uk/blog/google-analytics-new-feature-shared-segments/