Anirudh
Anirudh

Reputation: 3388

Google Search API v3 returning undefined as response

Here is the URL to which I'm making POST request:

https://www.googleapis.com/webmasters/v3/sites/http://mywebsite.com/searchAnalytics/query?key=key&startDate=2018-07-01&endDate=2018-09-01&dimensions=["country","device"]

I'm getting Not Found as response. I'm following exactly as docomunted by Google. What am I missing here.

Here is my code in Nodejs

request.post({
          url: url,
          auth: {
                  'bearer': access_token
                }
          }, function(err, response) {

           if(err)
           {
             res.status(500).send({ error: "google error", data: null, message: "Oops! Please try again" });

           }
           else {
               console.log(response);
           }

        }); 

Upvotes: 0

Views: 239

Answers (1)

Anirudh
Anirudh

Reputation: 3388

There were many things which needed to be corrected. Here is the final code which worked.

         website = stripTrailingSlash(website);
         website = encodeURIComponent(website);
         var url = 'https://www.googleapis.com/webmasters/v3/sites/'+website+'/searchAnalytics/query?key=yourKEY';

         console.log(url);

         var bearerToken = 'Bearer '+ access_token;

         var options = {
           url: url,
           headers: {
             'Authorization': bearerToken,
             'Accept': 'application/json',
             'Content-Type': 'application/json'
           },
           json: {'startDate':'2018-02-01','endDate':'2018-09-01'}
         };

         request.post(options, function(err, response) {

           if(err)
           {
             console.log(err);
             res.status(500).send({ error: "google error", data: null, message: "Oops! Please try again" });

           }
           else {
               console.log(response.body);

           }

        });

Upvotes: 0

Related Questions