Google Analytics嵌入API:如何取消访问令牌? [英] Google Analytics Embed API: How to retireve access token?

查看:91
本文介绍了Google Analytics嵌入API:如何取消访问令牌?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了此问题以获得对Google Analytics Embed API的访问权限。我想从我的网站上显示统计数据,而不需要具有正确权限的用户登录(所以没有登录屏幕)。

I've used the code provided in this question to gain access to the Google Analytics Embed API. I want to display the statistics from my website without the need for users with the correct privileges to log in (so no login screen).

因此,我已经创建了一个服务帐户并保存了p12文件。但是,以下代码显示空白页面。

For that reason, I've created a service account and saved the p12 file. However, the following code displays an empty page.

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

<section id="timeline"></section>

<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() {

  var IDS = 'ga:XXXX'; // I've hidden my personal ID for security purposes
  var ACCESS_TOKEN = 'key.p12'; // obtained from your service account

  gapi.analytics.auth.authorize({
    serverAuth: {
      access_token: ACCESS_TOKEN
    }
  });

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

});
</script>
</body>
</html>

也许Access Token不应该是p12文件?但是,如果是这样,它应该是什么?我真的迷了路。

Maybe the Access Token shouldn't be the p12 file? But if so, what should it be? I'm really lost.

推荐答案

您可以绝对使用带有Embed API的服务帐户。诀窍是从.p12文件中获取访问令牌,但是一旦您拥有有效的访问令牌,您的代码就可以正常工作。

You can absolutely use a service account with the Embed API. The trick is getting an access token from the .p12 file, but once you have a valid access token, your code will work just fine.

我刚刚验证了这一点我。以下是我采取的步骤:

I've just verified this myself. Here are the steps I took:

我创建了一个服务帐户,然后按照 google-oauth-jwt节点模块文档页面获取访问令牌。 (如果您不使用Node.js,只需执行Google搜索,了解其如何在其他语言中工作,

I created a service account, and then I followed the steps listed on the google-oauth-jwt node module documentation page to get an access token. (If you're not using Node.js, just do a Google search for how this works in other languages, this devguide describes the process for PHP.)

我将.p12文件转换为.pem文件(需要使用Node )使用以下命令:

I converted the .p12 file to a .pem file (required to work with Node) with this command:

openssl pkcs12 -in downloaded-key-file.p12 -out your-key-file.pem -nodes

我运行以下Node程序以从.pem文件获取访问令牌:

I ran the following Node program to get an access token from the .pem file:

var googleAuth = require('google-oauth-jwt');
var authOptions = {
  email: 'my-service-account@developer.gserviceaccount.com',
  keyFile: './key.pem',
  scopes: ['https://www.googleapis.com/auth/analytics']
};

googleAuth.authenticate(authOptions, function (err, token) {
  console.log(token);
});

一旦我拥有访问令牌,我就将它代入您在问题中的代码中,解雇建立一个本地服务器,一切正常。

Once I had the access token, I just substituted it into the code you have in your questions, fired up a local server, and everything worked just fine.

这篇关于Google Analytics嵌入API:如何取消访问令牌?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆