【问题标题】:OAuth Google Analytics Client API javascriptOAuth 谷歌分析客户端 API javascript
【发布时间】:2015-01-29 10:08:28
【问题描述】:

我正在尝试连接 Google 客户端 API 以在我的网站上显示 Google Analytics 数据。

所以我使用本教程尝试连接客户端 API: https://developers.google.com/api-client-library/javascript/start/start-js

我按照所有说明操作,在 Google 开发者控制台上创建了 OAuth 客户端。

这里是我的代码:

<button id="authorize-button">Authorize</button>
    <script type="text/javascript">

      var clientId = 'XXXXXXXXX';

      var apiKey = 'XXXXXXX';

      var scopes = 'https://www.googleapis.com/auth/plus.me';

      function handleClientLoad() {
        // Step 2: Reference the API key
        gapi.client.setApiKey(apiKey);

        window.setTimeout(checkAuth,1);
      }

      function checkAuth() {

        gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: false}, handleAuthResult);
      }

      function handleAuthResult(authResult) {

        var authorizeButton = document.getElementById('authorize-button');
        if (authResult && !authResult.error) {
          authorizeButton.style.visibility = 'hidden';

          makeApiCall();
        } else {
          authorizeButton.style.visibility = '';
          authorizeButton.onclick = handleAuthClick;
        }
      }

      function handleAuthClick(event) {
        // Step 3: get authorization to use private data
        gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: false}, handleAuthResult);
        return false;
      }

      // Load the API and make an API call.  Display the results on the screen.
      function makeApiCall() {

        // Step 4: Load the Google+ API
        gapi.client.load('plus', 'v1').then(function() {
          // Step 5: Assemble the API request
          var request = gapi.client.plus.people.get({
            'userId': 'me'
          });
          // Step 6: Execute the API request

          request.then(function(resp) {
            var heading = document.createElement('h4');
            var image = document.createElement('img');
            image.src = resp.result.image.url;
            heading.appendChild(image);
            heading.appendChild(document.createTextNode(resp.result.displayName));

            document.getElementById('content').appendChild(heading);
          }, function(reason) {
            console.log('Error: ' + reason.result.error.message);
          });
        });
      }
    </script>
    // Step 1: Load JavaScript client library
    <script src="https://apis.google.com/js/client.js?onload=handleClientLoad"></script>

但它不起作用,我不知道是什么问题,我得到这个错误:

401. That’s an error.

Error: invalid_client

The OAuth client was not found.

Request Details
immediate=false
response_type=token
scope=https://www.googleapis.com/auth/plus.me
redirect_uri=postmessage
proxy=oauth2relay509775948
state=658721539|0.1255556678
origin=http://localhost
include_granted_scopes=true
client_id='MY SECRED KEY'
authuser=0
That’s all we know.

如果有人有解决方案,那应该非常好;) 提前致谢。

【问题讨论】:

  • 无需向我们展示 API 密钥
  • 谢谢,忘记隐藏api键
  • 在开发者控制台的同意屏幕中,确保您添加了电子邮件地址和产品名称。看起来您正在使用的范围是 google+ 范围,它不是您必须更改的 Google 分析范围,但它与客户端错误消息无关。
  • 还要记住这是 Oauth2,您正在使用它需要身份验证。因此,如果其他用户访问您的网站,它不会显示您的 Google 分析数据,而是会显示他们的。

标签: javascript oauth google-analytics google-analytics-api google-client


【解决方案1】:

我错了,要显示来自 GAI 的内容,我需要使用带有 PHP 代码的 Hello 分析教程。 https://developers.google.com/analytics/solutions/articles/hello-analytics-api?hl=fr#auth

显然文档已经过时了。

谢谢...

猜你喜欢
  • 1970-01-01
  • 2017-05-26
  • 2021-11-19
  • 1970-01-01
  • 2017-10-28
  • 1970-01-01
  • 2019-05-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多