【问题标题】:Service Applications and Google Analytics API V3: Server-to-server OAuth2 authentication?服务应用程序和 Google Analytics API V3:服务器到服务器 OAuth2 身份验证?
【发布时间】:2012-04-09 10:45:04
【问题描述】:

我正在尝试制作一个服务器应用程序,以定期从我自己的 GA 帐户中提取 Google Analytics(分析)数据。请注意,它是访问我自己的数据的个人服务器端应用程序,即 没有最终用户访问此应用程序。

因此,我在Google API Console 中将我的应用程序注册为服务应用程序,它为我提供了一个客户端 ID 和一个私钥。据我了解,服务应用程序不使用 Application Secret 和 Redirect URL,因为在此服务器到服务器身份验证流程中没有最终用户。事实上,Google API 控制台没有给我任何秘密,也没有提示我输入重定向 URL。

不幸的是,我不知道如何在Google's PHP Client API 中验证我的服务应用程序。有大量关于与最终用户验证 Web 应用程序的文档。

Google 的文档建议使用 it is possible to authenticate server-to-server by signing a JWT request with the private key。我只是不知道如何在 PHP 客户端 API 中进行操作(尽管我已经浏览了源代码并且有 definitely a script 使用私钥签署请求。)

我在这里遗漏了什么吗?如何使用我的私钥和 Google PHP 客户端 API 对服务应用程序执行身份验证?

为清晰起见进行了编辑

【问题讨论】:

    标签: php google-api oauth-2.0 google-analytics-api jwt


    【解决方案1】:

    如果您使用的是Google's PHP client API,请转到Google API Console 并单击左侧的API Access。

    然后Create a Client ID。这将为您提供secret,这是您设置redirect URL 的地方。它不会为您提供重定向 URL - 即应用在验证后将用户发送回的 URL。

    有other authentication methods你可以看看。

    【讨论】:

    • 感谢您的回复。如前所述,我将我的应用程序用作 Google 所描述的 service account,即不涉及使用端。当我在 Google API 控制台中设置我的应用程序时,没有生成任何秘密,也没有提示我输入重定向 URL。这就说得通了;此应用程序流程中没有最终用户。另外,如果可能的话,我更愿意使用 Core Reporting API v3。
    【解决方案2】:

    Google API PHP 客户端现在支持中继上的服务帐户。

    实现尚未发布,因此您需要checkout 最新版本的 PHP 客户端。

    我准备了一个示例应用程序,演示如何使用服务帐户访问 Google Prediction API。 要查看示例,请查看示例/预测/serviceAccount.php 或访问: http://code.google.com/p/google-api-php-client/source/browse/trunk/examples/prediction/serviceAccount.php

    【讨论】:

    【解决方案3】:

    2012 年 7 月 21 日更新

    Google Analytics API V3 现在支持由 .p12 签名的 JWT 请求返回的 OAuth2 令牌。也就是说,我们现在可以使用带有服务帐户的 Analytics API。

    目前正在提取 4 年的日常指标,只是为了它。

    这是一个快速的“n”一步一步:

    1. 转到Google API Console 并创建一个新应用

    2. 在 服务 标签中,拨动 Google Analytics 开关

    3. 在 API 访问 选项卡中,单击 创建 OAuth2.0 客户端 ID

      • 输入您的姓名,上传徽标,然后单击下一步

      • 选择服务帐户选项并按创建客户ID

      • 下载您的私钥

    4. 现在您回到了API 访问 页面。您将看到一个名为 服务帐户 的部分,其中包含 客户 ID 和 电子邮件地址

      • 复制电子邮件地址(类似于 ####@developer.gserviceaccount.com)

      • 访问您的GA Admin 并将此电子邮件作为用户添加到您的属性中

      • 这是必须的;否则你会得到神秘的错误。

    5. 通过 Github 获取最新的Google PHP Client API

      git submodule add https://github.com/google/google-api-php-client.git google-api-php-client-read-only
      
    6. 摇滚乐(感谢所有关于更新类名的提示):

      // api dependencies
      require_once(PATH_TO_API . 'Google/Client.php');
      require_once(PATH_TO_API . 'Google/Service/Analytics.php');
      
      // create client object and set app name
      $client = new Google_Client();
      $client->setApplicationName(APP_NAME); // name of your app
      
      // set assertion credentials
      $client->setAssertionCredentials(
        new Google_Auth_AssertionCredentials(
      
          APP_EMAIL, // email you added to GA
      
          array('https://www.googleapis.com/auth/analytics.readonly'),
      
          file_get_contents(PATH_TO_PRIVATE_KEY_FILE)  // keyfile you downloaded
      
      ));
      
      // other settings
      $client->setClientId(CLIENT_ID);           // from API console
      $client->setAccessType('offline_access');  // this may be unnecessary?
      
      // create service and get data
      $service = new Google_Service_Analytics($client);
      $service->data_ga->get($ids, $startDate, $endDate, $metrics, $optParams);
      

     

    下面的原始解决方法


    似乎,尽管文档模棱两可,most Google APIs do not support service accounts yet,包括 Google Analytics。他们 无法消化 .p12 签名的 JWT 请求返回的 OAuth2 令牌。所以, 截至目前,您无法将 Google Analytics API V3 与 服务帐号。

    解决方法:

    1. 在Google API console 中,创建一个客户端应用程序。

    2. 按照Google PHP Client API 示例中的步骤,使用您的client_id、client_secret 生成client_auth_url, 和redirect_uri

    3. 使用 cURL 登录到 Google。 (一定要使用 cookie 文件!)

    4. 在 cURL 中打开 client_auth_url 并填写表格。确保设置 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); 和 curl_setopt($ch, CURLOPT_HEADER, 1); 作为authorization_code 将在响应的Location: 标头中。

    5. 使用您的 client_id、client_secret、redirect_uri 和第 4 步中的激活码,向 Google's OAuth2 Token machine 发送请求。确保在您的帖子字段中包含grant_type = "authorization_code"。

    6. 万岁,你现在有一个永不过期的refresh_token 和一个有效的access_token!使用您的client_id、client_secret、redirect_uri 向Google's OAuth2 Token machine 发送请求, 和refresh_token 当你的access_token 过期时,你会得到一个 新的。

    【讨论】:

    • @dubbe 点击上面的链接后,选择一个帐户,转到用户选项卡,然后按新用户按钮。
    • 刚下SVN的版本,有些地方发生了变化。 apiClient() -> Google_Client(), apiAssertionCredentials -> Google_AssertionCredentials, apiAnalyticsService -> Google_AnalyticsService.. 基本上,'api...' 现在是 'Google_...'。 (包含相同)
    • @rkarbowski 感谢您的原始回答 - 也帮助了我;)
    • 非常感谢!如果不是因为您的帖子,尤其是更新,我会浪费很多时间并且可能会放弃。我从“Hello Analytics API”教程 (developers.google.com/analytics/solutions/articles/…) 开始,我重用了他们的一些代码。对于那些这样做的人,不要忘记设置 $client->setUseObjects(true); @rkarbowski 我欠你一杯啤酒;)下次你在巴黎!
    • @rohan-patel 添加require_once 'path/to/src/Google/autoload.php';.
    【解决方案4】:

    您可以使用非常有用的 php 库 GAPI(Google Analytics API PHP 接口)在没有 OAuth 的情况下访问 Google Analytics。它很容易使用。

    【讨论】:

      猜你喜欢
      • 2016-01-02
      • 2019-07-23
      • 1970-01-01
      • 2015-03-23
      • 2020-04-22
      • 1970-01-01
      • 2017-08-15
      • 2016-05-16
      • 2022-11-30
      相关资源
      最近更新 更多