Koz*_*k12 31 php google-analytics-api jwt
我正在尝试通过服务帐户使用Google AnalyticsAPI获取数据.
我一直在搜索stackoverflow如何做到这一点,并使用几个帖子上找到的完全相同的代码,但遇到问题,让它工作.
资料来源:
服务应用程序和Google Analytics API V3:服务器到服务器OAuth2身份验证?
服务应用程序和Google AnalyticsAPI V3:错误101(net :: ERR_CONNECTION_RESET)
这就是我得到的:
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);
执行时我得到以下错误:
致命错误:未捕获的异常"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 /应用/ XAMPP/xamppfiles/htdocs中/见解/应用/库/ googleAPI/IO/Google_REST .PHP(36):Google_REST :: decodeHttpResponse(对象(Google_HttpRequest))#1 /Applications/XAMPP/xamppfiles/htdocs/insights/application/libraries/googleAPI/service/Google_ServiceResource.php(178):Google_REST ::执行(对象(Google_HttpRequest))#2 /Applications/XAMPP/xamppfiles/htdocs/insights/application/libraries/googleAPI/contrib/Google_AnalyticsService.php(383):Google_ServiceResource - > __呼叫( 'GET',阵列)#3 /应用/ XAMPP/xamppfiles/htdocs中/见解/应用/库/ Google.php(46):Google_DataGaServiceResource->的get('克/Applications/XAMPP/xamppfiles/htdocs/insights/application/libraries/googleAPI/io/Google_REST.php上线66
因为我使用与工作示例完全相同的代码,我认为它可能是API或服务帐户设置的问题?
这是我添加到分析帐户的用户:

这是api访问帐户:

有谁知道我做错了什么?
jar*_*e87 71
我得到完全相同的错误,这解决了我:
我使用的是帐户ID而不是View ID.切换到视图ID修复它 - 它始终是"视图ID"而不是帐户或属性ID(看起来像"UA-xxx").View ID适用于特定的Web属性,可在Admin-> View(第3列) - > View Settings中找到.它是一个没有破折号的整数.
jre*_*ard 10
今天有同样的问题,profileId很难找到,你可以在每个分析中使用 它来说https://www.google.com/analytics/web/#management/Settings/aXXXXXXwYYYYYYYpZZZZZZZ/这是ZZZZZZZ部分
同样的问题.通过使用VIEW ID而不是帐户ID(UA-XXXXX-1)解决了这个问题.
Analytics Console > Admin -> View (Profile) -> View Settings -> View ID
和
确保将服务帐户添加到Google Analytics控制台中的用户列表中.只需在开发人员控制台的凭据,API和权限部分进行设置,仍然无法授予其访问分析的权限.
Analytics Console > Admin -> Account -> User Management -> "Add permissions for:"
| 归档时间: | 
 | 
| 查看次数: | 21926 次 | 
| 最近记录: |