2016-08-29 5 views
0

Ich versuche Google Analytics API innerhalb eines Express/React/d3 Projekts aufzurufen. Ich bin nah dran, all das zu schaffen, aber ich muss mich immer noch mit async Callback auf dem jwtClient beschäftigen, um den GA API Aufruf zu machen. Wie Sie in meinem Code sehen, verwende ich ein promise mit bluebird mit meinem jwtClient beschäftigen jedoch habe ich folgende Fehlermeldung bekommen:'refreshToken_' Fehler mit "promisified" Google Analytics's JWT Client

Cannot read property 'refreshToken_' of undefined 

Da ich noch ein Anfänger bin, und ich bin nur zu entdecken das Konzept des Versprechens würde ich etwas Hilfe lieben.

Hier ist mein Code;

var google = require ("googleapis"); 
    var key = require ('./client_id.json'); 
    const Promise = require('bluebird'); 
    var authorizationPromise; 

    const VIEW_ID = 'ga:80820965'; 

    let jwtClient = new google.auth.JWT(
     key.client_email, 
     null, 
     key.private_key, 
     ['https://www.googleapis.com/auth/analytics.readonly'], 
     null 
    ); 

    authorizationPromise = Promise.promisify(jwtClient.authorize)() 
    .then(function (err, tokens) { 
     if (err) { 
     throw new Error(err); 
     } 

     return google.analytics('v3'); 
    }) 
    .catch(function(err) { 
     console.log(err); 
    }); 

    var queryData = function() { 
     authorizationPromise.then(function(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 
    }; 

Jemand mir helfen, diesen Code zu schreiben, aber ich verstehe nicht wirklich, diese tokens Variable ...

Dank.

Antwort

1

Try

Promise.promisify(jwtClient.authorize)() Wechsel

zu

Promise.promisify(jwtClient.authorize, {context:jwtClient})()

Auch Ihre .then(function(err,token) sollte nur .then(function(tokens) sein, da die err von der .catch

behandelt wird