Alan Mroczek Alan Mroczek - 1 year ago 121
Node.js Question

NodeJS & googleapis, POST request not working?

I've got a problem with google api in NodeJS with hapi. I downloaded files with npm install googleapis and google-auth-library. Everything is setup correctly.

function listData(auth) {
let webmasters = google.webmasters('v3');
webmasters.searchanalytics.query({
auth: auth,
siteUrl: 'http%3A%2F%2Falanmroczek.pl%2F',
startDate: "2016-09-20",
endDate: "2016-10-14"
}, function(err, response) {
if (err) {
console.log('The API returned an error: ' + err);
return;
}

console.log(response);
});
}


I'm authorised to that scope by this app (if not it throws an error, so I'm sure that this part is okey). When I done listing my gmail folders it works perfectly. Wrong may be only this part of code or googleapis. Strange for me is that:

When I console.log request to Google API:

protocol: 'https:',
slashes: true,
auth: null,
host: 'www.googleapis.com',
port: null,
hostname: 'www.googleapis.com',
hash: null,
search: '?startDate=2016-09-20&endDate=2016-10-14',
query: 'startDate=2016-09-20&endDate=2016-10-14',
pathname: '/webmasters/v3/sites/http%3A%2F%2Falanmroczek.pl%2F/searchAnalytics/query',
path: '/webmasters/v3/sites/http%3A%2F%2Falanmroczek.pl%2F/searchAnalytics/query?startDate=2016-09-20&endDate=2016-10-14',
href: 'https://www.googleapis.com/webmasters/v3/sites/http%3A%2F%2Falanmroczek.pl%2F/searchAnalytics/query?startDate=2016-09-20&endDate=2016-10-14' },


Query, path and href looks like normal GET, I have no idea why. I tried to override it but still I get "backend error".

EDIT: Gmail list folders via GET, thats why I pointed out POST.

Answer Source

It's probably issue with googleapis for node.js. It's sending data via GET not required POST and JSON. We have to do it manually.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download