I am trying to grab data from the YouTube Analytics API. My Meteor project involves users logging into the site using their Google accounts through OAuth 2.0 (I’m using the accounts-google package for this) and when signing in I need them to be able to see some data from the YouTube Analytics API for their YouTube channel.
I have added the proper scopes to my accounts-google login system, and I can see accessToken, idToken, expiresAt, refreshToken, etc. under services.
Using the “Try It” API Explorer on the YouTube Analytics API Documentation page, I can get the type of response I’m looking for through this request:
GET https://www.googleapis.com/youtube/analytics/v1/reports?ids=channel%3D%3DMINE&start-date=2015-10-01&end-date=2015-10-31&metrics=views&dimensions=day&sort=-day&key={YOUR_API_KEY}
It’s gotten a little messy but this is where I currently am at:
//// TestingClient.js ////
var params = {
'start-date': "2015-10-01",
'end-date': "2015-10-31",
'metrics': "views",
'dimensions': "day",
'sort': "-day"
};
var currentAccessToken = Meteor.user().services.google.accessToken;
var opts = {
channelId: "MINE",
currentAccessToken: currentAccessToken
};
Meteor.call('getYoutubeReports', opts.channelId, opts.currentAccessToken, params, function(error, result) {
console.log(result);
console.log(error);
});
//// In Server.js ////
Meteor.publish("userData", function () {
return Meteor.users.find({_id: this.userId},
{fields: {'services': 1}});
});
Meteor.publish(null, function() {
if (this.userId != null) {
return Meteor.users.find({
_id: this.userId
}, {
fields: {
'createdAt': 1,
'services': 1,
}
});
} else {
return this.ready();
}
});
Meteor.methods({
getYoutubeReports: function(channelId, access_token, params) {
params.ids = "channel=="+ channelId;
params.key = access_token;
return HTTP.get("https://www.googleapis.com/youtube/analytics/v1/reports",
{params:params});
}
});
With the code above all I receive is a ‘Invalid Credentials’ error in my console log.
I feel like there is probably a better way to go about what I’m trying to achieve but I’m really quite lost, any information/example code is extremely appreciated.
Edit: I forgot to mention I have also added the HTTP package for this.