Saturday, 15 April 2017

Node.js - Display my own google analytics data to visitors

I am struggling to authenticate on server side.

I would like to display my own data from Google Analytics to my site's visitors. Every manual, API, or tutorial I can find explains how to use OAUTH2 to authenticate users.

ie: https://github.com/google/google-api-nodejs-client#alpha https://developers.google.com/analytics/devguides/reporting/embed/v1/core-methods-reference https://developers.google.com/analytics/devguides/reporting/core/v2/authorization and so on.

With that, I do not need to authenticate users because it's not their accounts I would like to access, but rather my own.

This is what I am using:

var google = require('googleapis');
var scopes = ['https://www.googleapis.com/auth/analytics.readonly']
var OAuth2 = google.auth.OAuth2;
var oauth2Client = new OAuth2(auth.googleAPI);
var analytics = google.analytics("v3")
console.log(analytics.data.ga.get({
    ids:"ga:107290894",
    "start-date":"2016-01-01",
    "end-date":"2017-04-15",
    "metrics":'ga:sessions,ga:pageviews',
}))

But I get the following error:

{ [Error: Login Required]
code: 401,
errors: 
 [ { domain: 'global',
   reason: 'required',
   message: 'Login Required',
   locationType: 'header',
   location: 'Authorization' } ] }

through auth.googleAPI I am passing the client id, secret and public key, and every other piece of information that was in the JSON file I got from google when I created a Service Account Key.

What am I doing wrong?



via Michael Seltenreich

No comments:

Post a Comment