api - Google Analytics API - 与 Symfony2 集成

标签 api symfony google-analytics integration

我正在尝试通过 OAuth2 访问 Google Analytics API。

我做了什么:

  1. 打开开发者控制台> APIs and Auth > Credentials

  2. 创建新的客户端 ID

  3. 生成p12 key

  4. 复制服务器上的 key

  5. 打开谷歌分析页面 > 管理 > 帐户 > 用户管理

  6. 添加来自生成的客户端 ID 的电子邮件,例如:xxxxxxxx-xxxxxxxxxxxxxxx@developer.gserviceaccount.com

  7. 授予此电子邮件读取和分析权限

  8. 然后当我返回开发人员控制台时 > 许可。新电子邮件添加到具有编辑权限的服务帐户

  9. 重新检查 Google Analytics 是否已启用以及数据是否正在输入。

现在我已经安装了 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"

我尝试创建的查询是:

        $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;

作为响应,我收到此错误:

User does not have sufficient permissions for this profile.

当我打开开发者控制台 > 概述 > 1 小时(选项卡)

我注意到请求正在进入。

综上所述——我假设身份验证和查询是可以的,但用户无权获取任何类型的数据,这很奇怪,因为我已授予读取和分析权限 xxxxxxxx-xxxxxxxxxxxxxxx@developer.gserviceaccount.com

该异常的原因可能是什么?

最佳答案

我不知道你是否已经解决了这个问题。 我的解决方案是在分析帐户上使用 View ID 而不是帐户 ID。 View ID 位于 Google Analytics 管理面板设置的第三列。

对不起我的英语。

关于api - Google Analytics API - 与 Symfony2 集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29490459/

相关文章:

Symfony2 Ddeboer bundle : replace blank with 0s

php - JW Player Google Analytics Pro 跟踪

java - Google Analytics Android 跟踪变量

php - Sonata Admin 如何读取未映射的字段?

ruby - 回形针远程 URL

php - Paypal NVP PHP API $ACK ="FAILURE"

api - 从 API 中删除已弃用的功能需要多长时间?

mysql - Doctrine 2,join table 两次,如何获取它的实体?

google-analytics - 将 Google Analytics 添加到 CKeditor 工具栏按钮

laravel - 使用 Passport 和 Laravel 进行身份验证时返回访问 token 和用户