Google Search API v3返回未定义的响应

时间:2018-09-24 10:46:01

标签: google-api google-webmaster-tools

这是我发出POST请求的URL:

https://www.googleapis.com/webmasters/v3/sites/http://mywebsite.com/searchAnalytics/query?key=key&startDate=2018-07-01&endDate=2018-09-01&dimensions=[“国家”,“设备”]

我得到“未找到”作为回应。我完全遵循Google的要求。我在这里想念什么。

这是我在Node.js中的代码

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);
           }

        }); 

1 个答案:

答案 0 :(得分:0)

有许多事情需要纠正。这是有效的最终代码。

         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);

           }

        });