Google Analytics Embed API:如何停用访问令牌? [英] Google Analytics Embed API: How to retireve access token?

查看:22
本文介绍了Google Analytics Embed 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>

也许访问令牌不应该是 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 上搜索它在其他语言中的工作原理,本开发指南 描述了 PHP 的过程.)

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 Embed API:如何停用访问令牌?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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