[英]Adding a property in Google Analytics
我一直在努力向我的Google Analytics(分析)帐户添加媒体资源,现在已经停留了24个小时以上,而且我不知道自己做错了什么...每次尝试创建网络媒体资源时返回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
错误。 有人请指出正确的方向,谢谢。
您需要发送“帐户ID”而不是“视图ID”。
$analytics->management_webproperties->insert('123456', $property);// 123456 my View Id
您是否已申请Beta版访问权限?
Beta访问:在Web API,视图(配置文件)和目标资源的Management API中的写操作(例如,创建,更新,删除,修补)可在有限的Beta中作为开发人员预览使用。 如果您对使用这些功能感兴趣,请请求访问Beta。
请求访问表格: 此处
必填项目
如果您对Beta版的访问权限有所保留,那么您可能希望尝试在Beta版论坛上发帖,我不记得是否有人使用PHP代码。 但是我认为setWebsiteUrl可能是Property主体的一部分。 但是它似乎没有针对PHP的文档记录,因此我将不得不对其进行测试。
服务帐号
似乎您正在使用服务帐户,是否向服务帐户授予了to the Google Analytics Account you are trying to create a new web property for
在帐户级别的写访问权限? 我进行了测试,您可以将insert与已被授予访问权限的服务帐户一起使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.