gpt4 book ai didi

php - 服务应用程序和 Google Analytics API V3 : Server-to-server OAuth2 authentication?

转载 作者:IT老高 更新时间:2023-10-28 12:03:55 24 4
gpt4 key购买 nike

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

因此,我在 Google API Console 中注册了我的应用程序。作为一个服务应用程序,它给了我一个Client ID和一个Private Key。据我了解,服务应用程序不使用 Application SecretRedirect URL,因为在此服务器到服务器身份验证流程中没有最终用户。事实上,Google API 控制台没有给我任何 secret ,也没有提示我输入重定向 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 对服务应用程序执行身份验证?

为清晰起见进行了编辑

最佳答案

2012 年 7 月 21 日更新

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

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

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

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

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

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

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

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

    • 下载您的私钥

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

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

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

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

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

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

 

下面的原始解决方法


It seems that, despite ambiguous documentation, most Google APIs do not support service accounts yet, including Google Analytics. They cannot digest OAuth2 tokens returned by a .p12 signed JWT request. So, as of right now, you cannot use Google Analytics API V3 with a service account.

Workaround:

  1. In the Google API console, create a client application.

  2. Follow the steps in the Google PHP Client API examples to generate a client_auth_url using your client_id, client_secret, and redirect_uri

  3. Login to Google using cURL. (Be sure to use a cookie file!)

  4. Open the client_auth_url in cURL and complete the form. Make sure you set curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); and curl_setopt($ch, CURLOPT_HEADER, 1); as the authorization_code will be in the Location: header of the response.

  5. Using your client_id, client_secret, redirect_uri, and the activation code from Step 4, post a request to the Google's OAuth2 Token machine. Make sure you include grant_type =
    "authorization_code"
    in your post fields.

  6. Hurray, you now have a refresh_token that never expires, and a working access_token! Post a request to the Google's OAuth2 Token machine with your client_id, client_secret, redirect_uri, and refresh_token when your access_token expires and you'll get a new one.

关于php - 服务应用程序和 Google Analytics API V3 : Server-to-server OAuth2 authentication?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9863509/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com