How to authenticate with the Google Analytics v4 Reporting API

I'm having trouble authenticating the Google Analytics v4 Reporting API using the Node.js client library . I tried all methods starting with JWT (service tokens: JSON and P12), API Keys and OAuth 2.0, but I have never been authenticated successfully.

I activated the API in my developer console, created identifiers and granted the rights to my property and viewing Google Analytics. I successfully receive authorization and access token for my service account, but I donโ€™t know how to use it for authentication in the Google Analytics v4 reporting API.

I am stuck in front of error message 401: "The request does not have valid credentials." I tried using JWT, impersonating the user, but then the service account is unauthorized.

Using Node.js Client Library and JWT Authentication:

var google = require('googleapis.js'); var viewID = 'XXXXXXXXX'; // Google Analytics view ID var key = require('service_account.json'); // Service account var jwtClient = new google.auth.JWT(key.client_email, null, key.private_key, ['https://www.googleapis.com/auth/analytics.readonly'], null); var oauth2Client = new google.auth.OAuth2(); jwtClient.authorize(function(err, result) { if (err) { console.log('Unauthorize'); console.log(err); return; } oauth2Client.setCredentials({ access_token: result.access_token }); //Need to authenticate somewhere near here var analytics = google.analyticsreporting('v4'); //Or here var req = { reportRequests: [ { viewId: viewID, dateRanges: [ { startDate: '2016-05-01', endDate: '2016-06-30', },], metrics: [ { expression: 'ga:users', }, { expression: 'ga:sessions', },], },], }; //Maybe here analytics.reports.batchGet(req, function(err, response) { if (err) { console.log('Fail'); console.log(err); return; } console.log('Success'); console.log(response); } ); }); 

Previous releases of the Node.js client library seem to have a way of specifying the client, but it has disappeared, possibly outdated.

 withAuthClient(oauth2Client) 

I tried to pass a client or token in an API call or in a request, but no one works.

 google.analyticsreporting({ version: 'v4', auth: oauth2Client }); google.analyticsreporting({ version: 'v4', access_token: result.access_token }); 

Maybe this is a noob question, but I donโ€™t know how to do it, I donโ€™t see anything related to the authentication of Google Analytics v4 in the google api documentation or client library, and most of the examples I found use the Google Analytics v3 API.

If someone was able to successfully authenticate to the Google Analytics v4 Reporting API, please help: /

+8
authentication api google-analytics
source share
2 answers

I found out what I am missing:

  • Google APIs Client API Options Library:

     google.options({ auth: oauth2Client }); //this one is not very optional 
  • Unlike the Google Analytics Reporting API v4 documentation, requests using the client library should have headers to indicate the client for each request (thanks to CVarisco, who notices that the client library documentation is not very accurate ..):

     var request ={ 'headers': {'Content-Type': 'application/json'}, 'auth': oauth2Client, 'resource': req, }; 
+7
source share

Note also that the recommended authentication method with a server-side service account uses auth.getApplicationDefault.

https://developers.google.com/identity/protocols/application-default-credentials

We recommend that you use the default credentials for the application in any of the following circumstances:
... chick ...
- You get access to the API with data related to the cloud project, or otherwise tied to the entire application, and not to the user's personal data. For calls related to user data, it is better to use an authorization stream where the end user gives explicit consent to access (see Using OAuth 2.0 to access the Google APIs).

0
source share

All Articles