简体   繁体   中英

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.

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.

Backend Errors which are being returned from Google API calls will have 503 as its error code which means "Service Unavailable".

Check the various API's and its status here and ensure the service that you are trying to hit is up and running fine.

https://www.google.com/appsstatus#hl=en&v=status

Hope this helps!

i work on the similar code. This Reference helped me.

Just have a look: NodeJS & Goodgle Express API

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM