节点:通过服务帐户的Google Analytics数据 [英] Node: Google Analytics data via service account

查看:96
本文介绍了节点:通过服务帐户的Google Analytics数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:我已将代码转换为 NPM模块



我按照自己的方式创建了服务帐户的JWT令牌,我可以访问用户数据,但我想获取我的分析数据以使用它来驱动我网站上的内容。



注意:我不知道是否有比这更好的方法,但现有的npm工具似乎要求您输入密码。因此,我最终使用REST调用而不是 gapi 工具。



这是我得到的错误



  {error:
{errors:[[Object]],
code:403,
消息:'用户没有任何Google Analytics帐户'。}}

这里是我的代码使用它可以处理用户数据。

  var oauth2Client = new OAuth2(CLIENT_ID,CLIENT_SECRET,REDIRECT_URL); 

var scopes = [
'https://www.googleapis.com/auth/plus.me',
'https://www.googleapis.com/auth /analytics.readonly'
];

var d = new Date();
var seconds = d.getTime()/ 1000 + 60 * 59;

var SERVICE_CLIENT_ID =xxxxxxx-0h21osagsg02eqk45me6ts7jn3kf0vfr.apps.googleusercontent.com;
var SERVICE_EMAIL =xxxxxxx-0h21osagsg02eqk45me6ts7jn3kf0vfr@developer.gserviceaccount.com

var claim_set = {
iss:SERVICE_EMAIL,
scope:'https ://www.googleapis.com/auth/analytics.readonly',
aud:'https://www.googleapis.com/oauth2/v3/token',
exp:秒,
iat:秒
};

var algorithm = {alg:RS256,typ:JWT};
var private_key = fs.readFileSync('privatekey.pem');

var signature = jwt.sign(claim_set,private_key,{algorithm:algorithm.alg});

var post_obj = {
grant_type:urn:ietf:params:oauth:grant-type:jwt-bearer,
断言:签名
};

request.post({
url:'https://www.googleapis.com/oauth2/v3/token',
form:post_obj
},函数(err,data){
if(err)throw err;
var body = JSON.parse(data.body);
var token = body.access_token;
/ / console.log(token);
console.log(Token:,token);


var auth_obj = {$ b $'auth':{
'bearer':令牌
}
};
//此代码可以返回信息
// request.get('https://www.googleapis.com / plus / v1 / people / me',auth_obj,function(err,data){
// if(err)throw err;
// console.log(JSON.parse(data.body) );
//});

var report = {
'ids':'ga:78624107',
'start-date':'2014- 10-01',
'end-date':'2014-12-31',
'metrics':'ga:sessions,ga:bounces'
};
var report2 ='metrics = ga%253Ausers& star t-date = 2015-02-24& end-date = 2015-03-10& max-results = 50'
var report3 ='ids = ga:78624107& start-date = 2015-02-24& end-date = 2015-03-10& metrics = ga:users'

request.get('https://www.googleapis.com/analytics/v3/data/ga?'+report3 ,auth_obj,函数(err,data){
if(err)throw err;
console.log(JSON.parse(data.body));
});
});


解决方案

默认情况下,服务帐户没有Google Analytics帐户。

进入Google Analytics网站,管理部分以 ACCOUNT 级别的用户身份添加服务帐户电子邮件地址,它必须在帐户级别。然后服务帐户将有权读取您的Google分析数据。


Update: I've since turned the code into an NPM module.

I've worked my way through created a JWT token for a service account, and I can access user data, but I want to get to my analytics data to use it to drive content on my website.

Note: I don't know if there is a better way than this, but the the existing npm tools seem to require that you enter your password. As a result I end up using REST calls rather the gapi tools.

This is the error I get

{ error: 
   { errors: [ [Object] ],
     code: 403,
     message: 'User does not have any Google Analytics account.' } }

Here is the code I am using which is working for user data.

var oauth2Client = new OAuth2(CLIENT_ID, CLIENT_SECRET, REDIRECT_URL);

var scopes = [
  'https://www.googleapis.com/auth/plus.me',
  'https://www.googleapis.com/auth/analytics.readonly'
];

var d = new Date();
var seconds = d.getTime() / 1000 + 60*59;

var SERVICE_CLIENT_ID = "xxxxxxx-0h21osagsg02eqk45me6ts7jn3kf0vfr.apps.googleusercontent.com";
var SERVICE_EMAIL     = "xxxxxxx-0h21osagsg02eqk45me6ts7jn3kf0vfr@developer.gserviceaccount.com"

var claim_set = {
    "iss": SERVICE_EMAIL,
    "scope": 'https://www.googleapis.com/auth/analytics.readonly',
    "aud": 'https://www.googleapis.com/oauth2/v3/token',
    "exp":seconds,
    "iat":seconds
};

var algorithm = {"alg":"RS256","typ":"JWT"};
var private_key = fs.readFileSync('privatekey.pem');

var signature = jwt.sign(claim_set, private_key, { algorithm: algorithm.alg});

var post_obj = {
    grant_type: "urn:ietf:params:oauth:grant-type:jwt-bearer",
    assertion: signature
};

request.post({
    url:'https://www.googleapis.com/oauth2/v3/token',
    form: post_obj
}, function(err, data) {
    if (err) throw err;
    var body = JSON.parse(data.body);
    var token = body.access_token;
    // console.log(token);
    console.log("Token: ",token);


    var auth_obj = {
        'auth': {
            'bearer': token
        }
    };
    // THIS CODE DOES RETURN INFORMATION
    // request.get('https://www.googleapis.com/plus/v1/people/me', auth_obj, function(err, data) {
    //  if (err) throw err;
    //  console.log(JSON.parse(data.body));
    // });

    var report = {
        'ids': 'ga:78624107',
        'start-date': '2014-10-01',
        'end-date': '2014-12-31',
        'metrics': 'ga:sessions,ga:bounces'
    };
    var report2 = 'metrics=ga%253Ausers&start-date=2015-02-24&end-date=2015-03-10&max-results=50'
    var report3 = 'ids=ga:78624107&start-date=2015-02-24&end-date=2015-03-10&metrics=ga:users'

    request.get('https://www.googleapis.com/analytics/v3/data/ga?'+report3, auth_obj, function(err, data) {
        if (err) throw err;
        console.log(JSON.parse(data.body));
    });
});

解决方案

A service account by default does not have a Google Analytics account.

Go into Google Analytics website the Admin section add the Service account email address as a user at the ACCOUNT level, it must be at the account level. Then the service account will have access to read your Google analytics data.

这篇关于节点:通过服务帐户的Google Analytics数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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