我一直在努力为我的Google Analytics帐户添加属性,我现在已经坚持了24小时以上,而且我不知道我做错了什么......每次我尝试{{ 3}}它返回403 (insufficient permission)
错误。
这是我的代码:
$client = new Google_Client();
$accountName = "xxxxxxxxxxxcl@developer.gserviceaccount.com";// Email Address
$p12 = $applicationPath."/google-api-php-client-master/API-xxxxx-xxxxxxx.p12";
$client->setScopes(array(
'https://www.googleapis.com/auth/analytics',
'https://www.googleapis.com/auth/analytics.manage.users'
));
$client->setApplicationName('My App Sample');
$client->setClientId('xxxxxxxxxxxcl.apps.googleusercontent.com');// Client Id
$client->setAccessType('offline');
$client->setAssertionCredentials(new Google_Auth_AssertionCredentials(
$accountName,
array('https://www.googleapis.com/auth/analytics'),
file_get_contents($p12), 'xxxxxxxx')
);
$analytics = new Google_Service_Analytics($client);
try {
$property = new Google_Service_Analytics_Webproperty();
$property->setName('sample-property');
$analytics->management_webproperties->insert('123456', $property);// 123456 my View Id
} catch (apiServiceException $e) {
print 'There was an Analytics API service error '
. $e->getCode() . ':' . $e->getMessage();
} catch (apiException $e) {
print 'There was a general API error '
. $e->getCode() . ':' . $e->getMessage();
}
我尝试了一切,但我一直收到(403) Insufficient Permission
错误。有人请指出我正确的方向,谢谢。
答案 0 :(得分:1)
您需要发送帐户ID而不是视图ID。
$analytics->management_webproperties->insert('123456', $property);// 123456 my View Id
您是否申请过Beta版访问权限?
Beta版访问: 在Web Property,View(Profile)和Goal资源的Management API中编写操作(例如,创建,更新,删除,修补) 目前作为有限测试版的开发者预览版提供。如果你是 有兴趣使用这些功能,请求访问测试版。
请求访问表单:here
必填字段
如果您已经获得了测试版访问权限,那么您可能想尝试在测试版论坛上发帖,我不记得是否有人使用PHP代码。但我认为可能需要setWebsiteUrl作为Property主体的一部分。但它似乎没有为PHP记录,我将不得不测试它。
服务帐户
您似乎还在使用服务帐户,是否在帐户级to the Google Analytics Account you are trying to create a new web property for
授予了服务帐户写入权限?我做了一个测试,你可以使用插入与已经被授予访问权限的服务帐户。