0

I'm using NodeJS, Express and GoogleApis en Google-oauth-jwt to authenticate.

I'm having problems with getting data from my second promise function (getData).

I can succesfully retrieve a token, when I use res.json(myToken) in the getToken().then(function(myToken). However I'm having problems using this token in the getData(token) function.

When I'm finally able to retrieve data from the googleapis via the getData function,I'd like to save this data into a MongoDB, to reduce calls to the Google Analytics API.

These are the errors I get:

2018-01-11T09:46:55.063Z 5ac2c6f1-f6b4-11e7-a2e2-c50e9f880e28 token: XXXXXXXXXXXXXXX

2018-01-11T09:46:55.148Z 5ac2c6f1-f6b4-11e7-a2e2-c50e9f880e28 { Error: Login Required at RequestError.Error (native) at new RequestError (/var/task/node_modules/google-auth-library/lib/transporters.js:34:42) at Request._callback (/var/task/node_modules/google-auth-library/lib/transporters.js:96:27) at Request.self.callback (/var/task/node_modules/request/request.js:186:22) at emitTwo (events.js:106:13) at Request.emit (events.js:191:7) at Request. (/var/task/node_modules/request/request.js:1163:10) at emitOne (events.js:96:13) at Request.emit (events.js:188:7) at IncomingMessage. (/var/task/node_modules/request/request.js:1085:12) code: 401, errors: [ { domain: 'global', reason: 'required', message: 'Login Required', locationType: 'header', location: 'Authorization' } ] }

2018-01-11T09:46:55.153Z 5ac2c6f1-f6b4-11e7-a2e2-c50e9f880e28 (node:1) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Login Required

This is my code:

var express = require('express');
var router = express.Router();
var googleAuth = require('google-oauth-jwt');
var google = require('googleapis');
var app = express();

/* get GA data*/
router.get('/kpis', function(req, res, next) {
  // get token with googleapis library
  function getToken() {
       return new Promise(function(resolve,reject) {
         googleAuth.authenticate({
           email: 'serviceAcountemail@email.com',
           key: "getting key",
           scopes: ['https://www.googleapis.com/auth/analytics.readonly']
           }, function (err, token) {
            if (err){
              reject(err)
            } else
             resolve(token)
          });
       });
   }

   function getData(token){
     return new Promise(function(resolve,reject){
       var analytics = google.analytics('v3');
       var VIEW_ID = 'ga:profileid';
       analytics.data.ga.get({
           'auth': token,
           '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)
             reject(err)
           } else
             console.log('Response:',response)
             resolve(response)

       });
    });
   }

   getToken().then(function(myToken) {
     // get the token, make a call to google analytics api with token included in auth
     console.log('token:', myToken);
     getData(myToken).then(function(results){
        // show results on screen in json
        res.json(gaResults);
     });
   });
});
/* end of get GA data*/
module.exports = router;

My question:

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
Elvira
  • 1,410
  • 5
  • 23
  • 50
  • There is something wrong with your authentication code. You may want to follow one of the other tutorials and then change it to Analytics after https://developers.google.com/drive/v3/web/quickstart/nodejs – Linda Lawton - DaImTo Jan 11 '18 at 09:56
  • But I'm getting a token which is valid, which I should be able to use, like: auth: token in the call to google analytics. The only thing that the JWTClient in the tutorial does is returning a token. – Elvira Jan 11 '18 at 10:05
  • Then there is a problem with how you are applying the token. The access token needs to be sent with the request to google analytics I dont know enough about how node works to be able to help thats why i suggested you look though a working official tutorial to find your error. I cant see where you are applying the client id and secret so i find it strange that you are getting an authenticated token back. Which type of credentials are you using? – Linda Lawton - DaImTo Jan 11 '18 at 10:09

0 Answers0