简体   繁体   English

Google AnalyticsAPI - 与Symfony2集成

[英]Google Analytics API - Integration With Symfony2

I am trying to gain access to Google Analytics API through OAuth2. 我正在尝试通过OAuth2访问Google AnalyticsAPI。

What i did: 我做了什么:

  1. Open developers console > APIs and Auth > Credentials 打开开发人员控制台> API和Auth>凭据

  2. Create a new Client ID 创建新的客户端ID

  3. Generate p12 key 生成p12键

  4. Copy the key on the server 复制服务器上的密钥

  5. Open google analytics page > admin > Account > User Management 打开谷歌分析页面>管理>帐户>用户管理

  6. Add the email from generated Client ID, something like: xxxxxxxx-xxxxxxxxxxxxxxx@developer.gserviceaccount.com 添加生成的客户端ID中的电子邮件,例如:xxxxxxxx-xxxxxxxxxxxxxxx@developer.gserviceaccount.com

  7. Give to this email Read and Analyze permissions 给这封电子邮件阅读并分析权限

  8. Then when I go back to developers console > permission. 然后当我回到开发者控制台>权限。 The new email is added on Service accounts with Edit permissions 新电子邮件将添加到具有“编辑”权限的服务帐户中

  9. Recheck if Google Analytics is enabled and data is going in. 重新检查Google Analytics是否已启用且数据是否已进入。

Now I had installed widop/google-analytics-bundle and configure the bundle: 现在我已经安装了widop / google-analytics-bundle并配置了这个包:

widop_google_analytics:
    client_id:        "xxxxxxxx-xxxxxxxxxxxxxxx@developer.gserviceaccount.com"
    profile_id:       "ga:12345678"
    private_key_file: "mykey.p12"
    http_adapter:     "widop_http_adapter.curl"

And the query I try to create is: 我尝试创建的查询是:

        $profileId = 'ga:12345678';
        $query = new Query($profileId);

        $query->setStartDate(new \DateTime('-2months'));
        $query->setEndDate(new \DateTime());

        $query->setMetrics(array('ga:visits' ,'ga:bounces'));
        $query->setDimensions(array('ga:browser', 'ga:city'));

        $query->setSorts(array('ga:country', 'ga:browser'));

        $query->setFilters(array('ga:browser=~^Firefox'));

        $query->setSegment('gaid::10');

        $query->setStartIndex(1);
        $query->setMaxResults(10000);
        $query->setPrettyPrint(false);
        $query->setCallback(null);

        $clientId = 'xxxxxxxx-xxxxxxxxxxxxxxx@developer.gserviceaccount.com';
        $privateKeyFile = 'mykey.p12';
        $httpAdapter = new CurlHttpAdapter();

        $client = new Client($clientId, $privateKeyFile, $httpAdapter);
        $token = $client->getAccessToken();

        $service = new Service($client);
        $response = $service->query($query);

        return $response;

As a response I get this error: 作为回应,我收到此错误:

User does not have sufficient permissions for this profile.

When I open developers console > overview > 1 hour (tab) 当我打开开发者控制台>概述> 1小时(标签)

I had notice that requests are going in. 我注意到请求正在进行中。

From all that - I assume that authentication and query is OK but the user has no permissions to get any kind of data which is weird because I had granted Read and Analyze permissions to xxxxxxxx-xxxxxxxxxxxxxxx@developer.gserviceaccount.com 从所有这些 - 我认为身份验证和查询是正常的,但用户无权获取任何类型的数据,这是奇怪的,因为我已授予读取和分析权限xxxxxxxx-xxxxxxxxxxxxxxx@developer.gserviceaccount.com

What could by the reason for that exception? 那个例外的原因是什么?

I do not know if you already solved this issue. 我不知道你是否已经解决了这个问题。 The solution for me was use the view ID instead of account ID on the analytics account. 对我而言,解决方案是在分析帐户中使用视图ID而不是帐户ID。 The view ID is on the third column in settings, on Google Analytics administration panel. 视图ID位于Google Analytics管理面板上的设置的第三列。

Sorry for my english. 对不起我的英语不好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM