php “权限不足”谷歌分析 API 服务帐户

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14525565/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:27:34  来源:igfitidea点击:

"Not sufficient permissions" google analytics API service account

phpgoogle-analytics-apijwt

提问by Kozmk12

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

我正在尝试通过服务帐户使用 Google Analytics API 获取数据。

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

我一直在 stackoverflow 上搜索如何做到这一点,并一直在使用在几个帖子中找到的完全相同的代码,但遇到问题才能使其正常工作。

Sources:

资料来源:

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

服务应用程序和 Google Analytics API V3:服务器到服务器 OAuth2 身份验证?

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

服务应用程序和 Google Analytics API V3:错误 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 = '[email protected]';


$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?ids=ga%3Axxxx7777&start-date=2012-01-01&end-date=2013- 01-25&metrics=ga%3Apageviews: (403) 用户对此配置文件没有足够的权限。在 /Applications/XAMPP/xamppfiles/htdocs/insights/application/libraries/googleAPI/io/Google_REST.php:66 堆栈跟踪:#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('

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 : Analytics users

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

and this is api access accounts: api access

这是 api 访问帐户: 接口访问

Does anybody know what i'm doing wrong?

有谁知道我做错了什么?

回答by josephrace

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(看起来像“UA-xxx”)。查看 ID 用于特定的网络资产,可以在管理->查看(第 3 列)->查看设置中找到。它是一个没有破折号的整数。

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

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

回答by jrenouard

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/aXXXXXXwYYYYYYYpZZZZZZZ/这是 ZZZZZZZ 部分

回答by Jason

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

同样的问题。通过使用 VIEW id 而不是帐户 ID (UA-XXXXX-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.

确保将服务帐户添加到 Google Analytics Console 中的用户列表中。简单地在开发者控制台的凭据、API 和权限部分设置它仍然不会授予它访问您的分析的权限。

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

回答by Havrin

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.

遇到了同样的错误,但过了一会儿,我意识到我在我的谷歌浏览器(我自己的电子邮件地址)中使用了这个脚本,但 Analytics API 正在公司电子邮件上运行。所以解决方案可能是编辑你的谷歌浏览器的设置。只需断开与谷歌浏览器(设置)中的 gmail 帐户的连接并再次测试即可。

回答by Pablo Martinez

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

使用查看 ID 不是帐户 ID,在管理选项卡上,“查看设置”“查看 ID”。

回答by Leumas Naypoka

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

经过 2 周的尝试寻找解决方案后,对我帮助很大:

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

分析控制台 -> 管理 -> 查看(配置文件) -> 查看设置 -> 查看 ID

回答by Lee Gary

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

转到您的 google 分析管理站点并将服务帐户添加为用户并授予该帐户必要的权限

回答by user1695595

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

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

回答by Ravi Kumar

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

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

回答by hellojebus

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#management/Settings/a51343283w84330433 p87396224/%3Fm.page%3DAccountSettings/

The ID we are looking for follows the letter p.

我们要查找的 ID 跟在字母 p 之后。

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

在上面的例子中,有效的 ID 是:87396224