mixmaxhq/batchelor

Getting response { parts: [statusCode: '401', statusMessage: 'Unauthorized',], errors: 0 } with valid token for analytics

Closed this issue · 3 comments

Hello,
If i'm using the following code, everything is ok:

const google = require('googleapis');
const OAuth2Client = google.auth.OAuth2;
const tokenGA = {
    "access_token": "ya29.XXXX",
    "expires_in": 3600,
    "refresh_token": "YYYY",
    "token_type": "Bearer",
    "created": 1485874552
};
const oAuth2ClientGA = new OAuth2Client(webGA.client_id, webGA.client_secret, webGA.redirect_uris[0]);
const analytics = google.analytics({version: 'v3',auth: oAuth2ClientGA});
oAuth2ClientGA.setCredentials(tokenGA);
analytics.data.ga.get({
        'ids': VIEW_ID,
        'start-date': '14daysAgo',
        'end-date': 'today',
        'metrics': 'ga:users,ga:sessions,ga:bounceRate,ga:avgSessionDuration,ga:goalCompletionsAll,ga:pageviewsPerSession,ga:uniquePageviews,ga:goalConversionRateAll',
        'dimensions': 'ga:date,ga:hour,ga:channelGrouping',
        'filters': 'ga:date=~' + _today + '|' + _1weekAgo + '|' + _2weeksAgo + ';ga:sessions>0'
    }, function (err, response) {
        if (err) {
            next(err);
        }
        res.send(response);
    });

But with Batchelor i always get an error
This is the code i used:
```
var Batchelor = require('batchelor');

var batch = new Batchelor({
    'uri':'https://www.googleapis.com/batch',
    'method':'POST',
    'auth': {
        'bearer': tokensGA.access_token
    },
    'headers': {
        'Content-Type': 'multipart/mixed'
    }
});

batch.add({
    'method':'GET',
    'path':'/analytics/v3/data/ga?ids='+ VIEW_ID+ '&start-date=14daysAgo&end-date=today&metrics=ga:users,ga:sessions,ga:bounces,ga:sessionDuration,ga:goalCompletionsAll,ga:pageviewsPerSession,ga:uniquePageviews,ga:pageViews&dimensions=ga:date,ga:hour&filters=ga:date=~' + _today + '|' + _1weekAgo + '|' + _2weeksAgo + ';ga:sessions>0'
});
I'm getting the following response:

{ parts:
[ { statusCode: '401',
statusMessage: 'Unauthorized',
headers: [Object],
body: [Object] } ],
errors: 0 }


Thanks in advance for any help you can provide, really appreciate it.

Hey @mikeplavo, was this resolved in the end?

fleea commented

Hi, I also experienced the same problem. Can you share the solution?

@fleea what happens if you run the call in Postman with the same credentials/URL?

Google sending back an auth error will most likely be just that. Batchelor can pretty much be used with any API. It's authentication method is whatever you would use with any API.

Check in Postman and if Auth fails, it's not an issue with Batchelor.