I'm trying to use Google Analytics Core Reporting API from Node.js by following this tutorial http://www.2ality.com/2015/10/google-analytics-api.html. I'm launching the script directly from my terminal with babel-node mygaapiscript.js
. but I've got the following error :
/Users/me/Desktop/dashboard/dashboard-ga/node_modules/gtoken/lib/index.js:160
this.scope = options.scope.join(' ');
^
TypeError: Cannot read property 'join' of null
at GoogleToken._configure (/Users/me/Desktop/dashboard/dashboard-ga/node_modules/gtoken/lib/index.js:160:31)
at new GoogleToken (/Users/me/Desktop/dashboard/dashboard-ga/node_modules/gtoken/lib/index.js:19:8)
at JWT.GoogleToken [as gToken] (/Users/me/Desktop/dashboard/dashboard-ga/node_modules/gtoken/lib/index.js:17:12)
at JWT._createGToken (/Users/me/Desktop/dashboard/dashboard-ga/node_modules/google-auth-library/lib/auth/jwtclient.js:218:24)
at JWT.refreshToken_ (/Users/me/Desktop/dashboard/dashboard-ga/node_modules/google-auth-library/lib/auth/jwtclient.js:133:15)
at JWT.authorize (/Users/me/Desktop/dashboard/me-MacBook-Pro:routes
What does this error mean ?
here is my script :
#!/usr/bin/env babel-node
import google from 'googleapis';
'use strict';
import key from './client_id.json';
const VIEW_ID = 'ga:70810323';
let jwtClient = new google.auth.JWT(
key.client_email,
key.private_key,
['https://www.googleapis.com/auth/analytics.readonly'],null);
jwtClient.authorize(function (err, tokens) {
if (err) {
console.log(err);
return;
}
let analytics = google.analytics('v3');
queryData(analytics);
});
function queryData(analytics) {
analytics.data.ga.get({
'auth': jwtClient,
'ids': VIEW_ID,
'metrics': 'ga:uniquePageviews',
'dimensions': 'ga:pagePath',
'start-date': '30daysAgo',
'end-date': 'yesterday',
'sort': '-ga:uniquePageviews',
'max-results': 10,
}, function (err, response) {
if (err) {
console.log(err);
return;
}
console.log(JSON.stringify(response, null, 4));
});
}
module.exports = {queryData};
Got the module.exports
because I'm using it inside express.
Your arguments to google.auth.JWT()
seem to be wrong. See the example in the documentation and the JWT example in the googleapis
repo here. The third argument should be the literal key data if you are not passing a key filename for the second argument. So just add null
for the third argument:
let jwtClient = new google.auth.JWT(
key.client_email,
key.private_key,
null,
['https://www.googleapis.com/auth/analytics.readonly'],
null
);