Json 谷歌分析报告API返回空对象-NodeJs
第一个问题,请耐心听我说 我使用NodeJS查询谷歌分析报告API。我能够接收到我需要的OAuth 2令牌,当我查询API时,我得到了一个200响应。但是,返回的负载是一个空对象,而不是预期目标的JSON格式的报告响应Json 谷歌分析报告API返回空对象-NodeJs,json,node.js,google-analytics,google-analytics-api,google-api-nodejs-client,Json,Node.js,Google Analytics,Google Analytics Api,Google Api Nodejs Client,第一个问题,请耐心听我说 我使用NodeJS查询谷歌分析报告API。我能够接收到我需要的OAuth 2令牌,当我查询API时,我得到了一个200响应。但是,返回的负载是一个空对象,而不是预期目标的JSON格式的报告响应 var https = require('https'); var google = require('googleapis'); var key = require('path/to/key'); var jwtClient = new google.auth.JWT(key.
var https = require('https');
var google = require('googleapis');
var key = require('path/to/key');
var jwtClient = new google.auth.JWT(key.client_email,null,key.private_key,'https://www.googleapis.com/auth/analytics.readonly',null);
var getGoogleData = google.analyticsreporting('v4');
var googleTemplates = {"reportRequests":[{"viewId": "######","dateRanges": [{"startDate": "2014-11-01", "endDate": "2014-11-30"}],"metrics": [{"expression": "ga:users"},{"expression": "ga:newUsers"},{"expression": "ga:pageviews / ga:sessions"}]},{"viewId": "######","dateRanges": [{"startDate": "2014-11-01", "endDate": "2014-11-30"}],"metrics": [{"expression": "ga:transactionRevenue"},{"expression": "ga:transactions"},{"expression":"ga:transactions / ga:sessions"},{"expression":"ga:revenuePerTransaction"}]}]};
var googleToken={};
var requestReport = function(reportRequest,token){
reportRequest = JSON.stringify(reportRequest);
//console.log(reportRequest);
var requestObject = {
method:'POST',
hostname:'analyticsreporting.googleapis.com',
path:'/v4/reports:batchGet',
headers:{
Accept:'*/*',
Authorization:'Bearer '+token.access_token,
'Content-Type':'application/x-www-form-urlencoded'
}
};
var callbackGoogle = function(response){
console.log('\n-----------------------\n');
console.log('Requesting Report : Google Analytics\nStatus Code: [', response.statusCode +': '+ response.statusMessage+']');
console.log('-----------------------\n\n');
var data = [];
response.on('data',function(chunk){
data.push(chunk);
});
response.on('end',function(){
var buff = new Buffer(data.join('')).toString();
console.log('////////////////////////// Success //////////////////////////\n')
console.log(buff);
});
response.on('error',function(e){
console.log(e);
});
};
var req = https.request(requestObject,callbackGoogle);
req.on('error',function(e){
console.log('requestReport Error:\n',e);
});
req.write(reportRequest);
req.end();
};
(function googleAccess(){
jwtClient.authorize(function(err,tokens){
console.log('\n-----------------------\n');
console.log('Authenticate: Google \n');
if(err){
console.log('Google Error',err);
return;
}
googleToken = tokens;
requestReport(googleTemplates,tokens);
console.log('Success: true');
console.log('\n-----------------------\n\n');
})
})();
控制台输出如下所示:
-----------------------
Authenticate: Google
Success: true
-----------------------
-----------------------
Requesting Report : Google Analytics
Status Code: [ 200: OK]
-----------------------
////////////////////////// Success //////////////////////////
{}
是否有人对有效负载作为空对象返回的原因提出了建议?它应该是一个报告JSON文件。我发现了问题!把这一条写在“不见林而不见树”下
requestObject
应该有'Content-type':'application/json'
我发现了问题!把这一条写在“不见林而不见树”下
requestObject
应该有'Content-type':'application/json'
您应该尝试一下console.log(response)
以查看整个输出。看起来您已经安装了google API,甚至在代码中有analyticsreporting服务对象(getGoogleData
),但您没有使用它;有什么原因吗?我确实试过了。我可以在周一发布整个回复对象。我不使用analyticsreporting服务对象的原因是它的文档非常稀少,我对附加的方法getGoogleData.reports.batchGet()
希望传递给它的内容感到有些困惑。我决定手动尝试该过程,但只是在尝试了batchGet first方法之后。当我使用附加到服务的方法时,我收到一个“NULL”响应,Google记录401和403错误或什么都没有。欢迎提供有关batchGet方法的信息!谢谢更多示例:使用默认javascript库编写。以serviceobject.method(requestbody).execute(callbackmethod)
格式写入,也可以使用服务对象写入。如果默认方法返回403
或401
,可能是因为Auth设置不正确,或者您使用了错误的viewID
。这只是两个最常见的错误,但您的请求同样可能有问题。从上面的模板中,您正试图在一批get中发出两个请求。我首先调试一个请求,然后添加额外的reportRequest
对象以查看整个输出。看起来您已经安装了google API,甚至在代码中有analyticsreporting服务对象(getGoogleData
),但您没有使用它;有什么原因吗?我确实试过了。我可以在周一发布整个回复对象。我不使用analyticsreporting服务对象的原因是它的文档非常稀少,我对附加的方法getGoogleData.reports.batchGet()
希望传递给它的内容感到有些困惑。我决定手动尝试该过程,但只是在尝试了batchGet first方法之后。当我使用附加到服务的方法时,我收到一个“NULL”响应,Google记录401和403错误或什么都没有。欢迎提供有关batchGet方法的信息!谢谢更多示例:使用默认javascript库编写。以serviceobject.method(requestbody).execute(callbackmethod)
格式写入,也可以使用服务对象写入。如果默认方法返回403
或401
,可能是因为Auth设置不正确,或者您使用了错误的viewID
。这只是两个最常见的错误,但您的请求同样可能有问题。从上面的模板中,您正试图在一批get中发出两个请求。我会先调试一个请求,然后添加额外的reportRequest
对象。你为什么不使用google的node js sdk?这项任务涉及整合google Analytics和Adobe Analytics的数据。最终我确实使用了谷歌的一个库,但我记得我必须做一些数据打包练习,以使项目仪表板同时显示这两个数据源。你为什么不使用谷歌的node js sdk?这项任务涉及整合谷歌分析和Adobe分析的数据。最终,我确实使用了谷歌的一个库,但我记得我必须做一些数据打包练习,才能让项目仪表板同时显示这两个数据源。