• <small id='2g00C'></small><noframes id='2g00C'>

  • <legend id='2g00C'><style id='2g00C'><dir id='2g00C'><q id='2g00C'></q></dir></style></legend>

        <bdo id='2g00C'></bdo><ul id='2g00C'></ul>
      1. <i id='2g00C'><tr id='2g00C'><dt id='2g00C'><q id='2g00C'><span id='2g00C'><b id='2g00C'><form id='2g00C'><ins id='2g00C'></ins><ul id='2g00C'></ul><sub id='2g00C'></sub></form><legend id='2g00C'></legend><bdo id='2g00C'><pre id='2g00C'><center id='2g00C'></center></pre></bdo></b><th id='2g00C'></th></span></q></dt></tr></i><div id='2g00C'><tfoot id='2g00C'></tfoot><dl id='2g00C'><fieldset id='2g00C'></fieldset></dl></div>
        <tfoot id='2g00C'></tfoot>
      2. 服务应用程序和 Google Analytics API V3:服务器到服务

        时间:2023-10-02

          <tbody id='QBYVN'></tbody>
            <i id='QBYVN'><tr id='QBYVN'><dt id='QBYVN'><q id='QBYVN'><span id='QBYVN'><b id='QBYVN'><form id='QBYVN'><ins id='QBYVN'></ins><ul id='QBYVN'></ul><sub id='QBYVN'></sub></form><legend id='QBYVN'></legend><bdo id='QBYVN'><pre id='QBYVN'><center id='QBYVN'></center></pre></bdo></b><th id='QBYVN'></th></span></q></dt></tr></i><div id='QBYVN'><tfoot id='QBYVN'></tfoot><dl id='QBYVN'><fieldset id='QBYVN'></fieldset></dl></div>

            <small id='QBYVN'></small><noframes id='QBYVN'>

              <bdo id='QBYVN'></bdo><ul id='QBYVN'></ul>
              1. <tfoot id='QBYVN'></tfoot>

                  <legend id='QBYVN'><style id='QBYVN'><dir id='QBYVN'><q id='QBYVN'></q></dir></style></legend>
                1. 本文介绍了服务应用程序和 Google Analytics API V3:服务器到服务器 OAuth2 身份验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

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

                  I'm trying to make a server application to routinely pull Google Analytics data from my own GA account. Note, it is a personal, server-side application accessing my own data, i.e. there is no end-user accessing this application.

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

                  As such, I registered my application in the Google API Console as a Service Application, which gave me a Client ID and a Private Key. It is my understanding that Service Applications do NOT use Application Secret and Redirect URL as there is no end-user in this server-to-server authentication flow. Indeed, the Google API Console gave me no Secret and did not prompt me for a Redirect URL.

                  不幸的是,我无法弄清楚如何在 Google 的内部验证我的服务应用程序PHP 客户端 API.有大量关于向最终用户验证 Web 应用程序的文档.

                  Unfortunately, I can not figure out how to authenticate my Service Application within Google's PHP Client API. There is extensive documentation on authenticating web applications with an end-user.

                  Google 的文档建议 可以通过使用私钥.我只是不知道如何在 PHP 客户端 API 中执行(尽管我已经浏览了源代码并且有 绝对是一个用私钥签署请求的脚本.)

                  Google's documentation suggests it is possible to authenticate server-to-server by signing a JWT request with the private key. I just can't figure out how to do within the PHP client API (although I've browsed the source and there's definitely a script that signs a request with the private key.)

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

                  Am I missing something here? How can I perform authentication for a Service Application with my private key and the Google PHP client API?

                  为清晰起见进行了编辑

                  推荐答案

                  2012 年 7 月 21 日更新

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

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

                  Currently pulling 4 years of day-by-day metrics, just for the hell of it.

                  这是一个快速的n"步骤:

                  Here's a quick 'n' dirty step-by-step:

                  1. 转到 Google API 控制台并创建一个新应用

                  服务标签中,打开Google Analytics开关

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

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

                  选择服务帐户选项,然后按创建客户 ID

                  下载您的私钥

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

                  Now you're back on the API Access page. You'll see a section called Service account with a Client ID and Email address

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

                  访问您的GA管理员并将此电子邮件添加为用户到您的属性

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

                  This is a must; you'll get cryptic errors otherwise.

                  通过 Github 获取最新的 Google PHP 客户端 API

                  Get the latest Google PHP Client API via Github

                  git submodule add https://github.com/google/google-api-php-client.git google-api-php-client-read-only
                  

                2. 摇滚乐(感谢大家提供有关更新类名称的提示):

                3. Rock 'n' roll (thanks all for tips on updated class names):

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

                4.  

                  下面的原始解决方法

                  似乎,尽管文档含糊不清,大多数 Google API 都可以尚不支持服务帐户,包括 Google Analytics.他们无法消化 .p12 签名的 JWT 请求返回的 OAuth2 令牌.所以,就目前而言,您不能将 Google Analytics API V3 与服务帐号.

                  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.

                  解决方法:

                  1. 在 Google API 控制台中,创建一个 客户端应用程序.

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

                  按照 Google PHP Client API 示例中的步骤生成client_auth_url 使用您的 client_idclient_secret、和 redirect_uri

                  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

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

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

                  在 cURL 中打开 client_auth_url 并完成表单.确保你设置了 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);curl_setopt($ch, CURLOPT_HEADER, 1); 作为authorization_code将位于响应的 Location: 标头中.

                  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.

                  使用您的 client_idclient_secretredirect_uri 和第 4 步中的激活码,向 Google 的 OAuth2令牌机.确保包含 grant_type =authorization_code" 在您的帖子字段中.

                  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.

                  万岁,您现在拥有一个永不过期的 refresh_token 和一个有效的 access_token!向 Google 的 OAuth2 令牌发布请求机器和你的client_idclient_secretredirect_uri,和 refresh_token 当您的 access_token 到期时,您将获得新的.

                  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.

                  这篇关于服务应用程序和 Google Analytics API V3:服务器到服务器 OAuth2 身份验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何在 MVC 框架中有效地实现模块,并在单个模块 下一篇:谷歌身份验证:OAuth2 不断返回“invalid_grant"

                  相关文章

                  最新文章

                  <small id='renD1'></small><noframes id='renD1'>

                  <tfoot id='renD1'></tfoot>
                  <i id='renD1'><tr id='renD1'><dt id='renD1'><q id='renD1'><span id='renD1'><b id='renD1'><form id='renD1'><ins id='renD1'></ins><ul id='renD1'></ul><sub id='renD1'></sub></form><legend id='renD1'></legend><bdo id='renD1'><pre id='renD1'><center id='renD1'></center></pre></bdo></b><th id='renD1'></th></span></q></dt></tr></i><div id='renD1'><tfoot id='renD1'></tfoot><dl id='renD1'><fieldset id='renD1'></fieldset></dl></div>
                    <bdo id='renD1'></bdo><ul id='renD1'></ul>

                      <legend id='renD1'><style id='renD1'><dir id='renD1'><q id='renD1'></q></dir></style></legend>