我需要通过Google分析授权才能获取响应数据 .
var google = require('googleapis'),
q = require('q'),
SERVICE_ACCOUNT_EMAIL = '838823084353-cjjoiv9di67fuh7geqgggociibataf9v@developer.gserviceaccount.com',
SERVICE_ACCOUNT_KEY_FILE = __dirname + '/google-services-private-key.pem';
var def = q.defer();
var gAnalytics = google.analytics('v3');
var authClient = new google.auth.JWT( SERVICE_ACCOUNT_EMAIL, SERVICE_ACCOUNT_KEY_FILE, null, ['https://www.googleapis.com/auth/analytics.readonly']);
console.log(authClient)
authClient.authorize(function (err, tokens) {
if (err) {
console.log("err is: " + err, tokens);
return;
}
但它无法授权
得到错误
JWT {transporter:DefaultTransporter {},clientId_:undefined,clientSecret_:undefined,redirectUri_:undefined,opts:{},credentials:{refresh_token:'jwt-placeholder',expiry_date:1},email:'838823084353-cjjoiv9di67fuh7geqgggociibataf9v @ developer .gserviceaccount.com',keyFile:'/ home / aaa / Desktop / ampretailer / server / google-services -private-key.pem',key:null,scopes:['https://www.googleapis.com/auth /analytics.readonly'],主题:未定义,gToken:[功能:GoogleToken]}错误是:错误:invalid_grant:无效的JWT签名 . {access_token:null,token_type:'Bearer',expiry_date:null}
1 回答
我建议您尝试使用Google Analytics v4而不是v3,您可以使用V3访问许多维度和指标 .
代码从analyticsReporting/batchGet.js撕掉
代码从samples/jwt.js撕掉