node.jsyoutube-apiyoutube-analytics-apiyoutube-analytics

Getting invalid_request for Youtube Analytics with nodejs library


I am trying to setup nodejs with youtube analytics api. I am currently using a refresh token to try and get access tokens. It works great when using postman but I can't seem to replicate the functionality in nodejs and get a 400: invalid_request with no additional information provided.

Here is my code

var google = require('googleapis');
var OAuth2 = google.auth.OAuth2;
var oAuthClient = new OAuth2();

// Retrieve tokens via token exchange explained above or set them:
oAuthClient.setCredentials({
   access_token: "",
   refresh_token: process.env["YOUTUBE_ANALYTICS_REFRESHTOKEN"]
});

var youtubeAnalytics = google.youtubeAnalytics({
   version: 'v1', auth: oAuthClient
});


var moduleExports = {
    retrieveDailyBreakdownViews : function(){   
       var query = {
          ids: 'channel==' + {channelID here},
          'start-date': '2017-05-01',
          'end-date': '2017-05-02',
           metrics: 'views,estimatedMinutesWatched',
           dimensions: 'insightPlaybackLocationType',
           sort: 'views'
       }
  youtubeAnalytics.reports.query(query, (error, response) => {
    console.log(error);
    console.log(response);
  });
}
}

module.exports = moduleExports;

Any ideas? If this doesn't work I can just try and build the query through HTTP/REST but I'd prefer to use the SDK.


Solution

  • This is what I ended up doing to fix the issue

    var google = require('googleapis');
    var googleAuth = require('google-auth-library');
    var auth = new googleAuth();
    var oauth2Client = new auth.OAuth2(process.env["YOUTUBE_CLIENT_ID"], 
    process.env["YOUTUBE_CLIENT_SECRET"]);
    oauth2Client.credentials.refresh_token = 
    process.env["YOUTUBE_ANALYTICS_REFRESHTOKEN"];
    
    var youtubeAnalytics = google.youtubeAnalytics({
        version: 'v1'
    });
    

    I then make my calls like this:

    youtubeAnalytics.reports.query(query, (error, response) => {})