0

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.

Simon Breton
  • 2,638
  • 7
  • 50
  • 105

1 Answers1

1

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
);
mscdex
  • 104,356
  • 15
  • 192
  • 153
  • Ok thanks. I did read the documentation and the JWT example but since It wasn't working, I guess I messed up the code a bit. However now I've got a brand new error `at Error (native) errno: -63, code: 'ENAMETOOLONG', syscall: 'open',` about my private key. – Simon Breton Aug 29 '16 at 16:40
  • 1
    Does `key.private_key` contain the *actual* private key data or is it a filename? If it's actual key data, then you will need to swap the second and third arguments. – mscdex Aug 29 '16 at 16:49
  • ok ok. I'm now understand the documentation. this point wasn't clear to me. I've got the following error `User does not have any Google Analytics account` so I guess it's working. Just need to play a bit with my GA account. thanks ! – Simon Breton Aug 29 '16 at 16:52