从 Google 分析 API 获取数据

Fetch data from Google Analytics API

我想要的:该网站应显示未经授权的分析数据。

我做了什么: 我在 google 应用程序引擎上有一个应用程序并启用了 API 并创建了服务帐户,它提供了 json文件。

我尝试做同样的事情:https://ga-dev-tools.appspot.com/embed-api/custom-components/但没有成功。

然后我遇到了这个问题:http://code.google.com/p/analytics-issues/issues/detail?id=496 并将我的代码更改为这个

<!DOCTYPE html>
<html>
<head>
  <title>Embed API Demo</title>
</head>
<body>

<!-- Step 1: Create the containing elements. -->

<section id="auth-button"></section>
<section id="view-selector"></section>
<section id="timeline"></section>

<!-- Step 2: Load the library. -->

<script>
(function(w,d,s,g,js,fjs){
  g=w.gapi||(w.gapi={});g.analytics={q:[],ready:function(cb){this.q.push(cb)}};
  js=d.createElement(s);fjs=d.getElementsByTagName(s)[0];
  js.src='https://apis.google.com/js/platform.js';
  fjs.parentNode.insertBefore(js,fjs);js.onload=function(){g.load('analytics')};
}(window,document,'script'));
</script>

<script>
gapi.analytics.ready(function() {

  // Step 3: Authorize the user.


  gapi.analytics.auth.authorize({
    serverAuth: '<server auth key>' 
  });

  // Step 4: Create the view selector.

  var viewSelector = new gapi.analytics.ViewSelector({
    container: 'view-selector'
  });

  // Step 5: Create the timeline chart.

  var timeline = new gapi.analytics.googleCharts.DataChart({
    reportType: 'ga',
    query: {
      'dimensions': 'ga:date',
      'metrics': 'ga:sessions',
      'start-date': '30daysAgo',
      'end-date': 'yesterday',
    },
    chart: {
      type: 'LINE',
      container: 'timeline'
    }
  });

  // YOU MUST CALL THIS MANUALLY HERE INSTEAD OF WAITING FOR CALLBACK
  viewSelector.execute();

  // Step 6: Hook up the components to work together.

  gapi.analytics.auth.on('success', function(response) {
    viewSelector.execute();
  });

  viewSelector.on('change', function(ids) {
    var newIds = {
      query: {
        ids: ids
      }
    }
    timeline.set(newIds).execute();
  });
});
</script>
</body>
</html>

我尝试查看一些文档来获取 服务器授权密钥 ,但我没有获取到它。

任何人都可以帮助设置服务器身份验证密钥,这样我显示未经身份验证的图表的目的就会得到解决吗?

谢谢, 沙拉德·索尼

Embeded API Getting started

The Embed API handles almost all of the authorization process for you by providing a one-click sign-in component that uses the familiar OAuth 2.0 flow. In order to get this button working on your page you'll need a client ID.

嵌入式 API 设计用于与 Oauh2 一起使用并要求用户进行身份验证,它不适用于服务帐户。我从未见过在 JavaScript 中工作的服务帐户的代码。这可能是因为包括我在内的很多人都不认为客户端 JavaScript 中的服务帐户是安全的,甚至可能违反 Goggles 的新使用条款。

如果您想使用服务帐户,您需要切换到服务器端语言进行身份验证。嵌入式 API 仅使用 Google 图表,因此您也可以手动对其进行编码。