Search code examples
node.jsgoogle-apigoogle-search-consolegoogle-api-nodejs-clientgoogle-api-webmasters

Google Search Console API, google.webmasters.searchanalytics.query "startDate field is required"


I am trying to use the Google Search Console API (nodejs) to retrieve the queries report. We own a google account where all my company's domains are configured. We would like to retrieve the complete list of domains from the api and then get the data from each of that domains.

We are able to correctly get the full list of domains. But we can get any data of them.

This is a brief example of the code.

// auth is the json web token
// domain is the url of the managed domain, example: https://www.asdfg.hif

async function getDomainData(auth, domain){
    p = {
        auth        : auth,
        siteUrl     : domain,
        startDate   : '2019-03-01',
        endDate     : '2019-03-31'
    };

    try{
        portalData = await google.webmasters('v3').searchanalytics.query(p);

        console.log( portalData );

        return portalData ;
    }catch(error){
        console.log('Error %s: %s', domain, error);
        return null;
    }
}//getDomainData

But I am always getting the following error. Which is indeed quite self-explanatory. But I cannot undertand it because I am providing the startDate and the endDate parameters in the p object. I have tried different date formats, single quotes, double quotes, no quotes ... No matter what I change, I always get the required fields error.

GaxiosError: startDate field is required.
GaxiosError: endDate field is required.

I can see the errors in the Google Search API console, so I think the error cames from the server, not from something in my code.

From the API Explorer I can test the api with no errors.

I do not know what can it be, but it seems to be something very silly.


Solution

  • How about this modification?

    At googleapis of Node.js, the request body is put in resource. So in your case, startDate and endDate are put in resource.

    From:

    p = {
        auth        : auth,
        siteUrl     : domain,
        startDate   : '2019-03-01',
        endDate     : '2019-03-31'
    };
    

    To:

    p = {
      auth: auth,
      siteUrl: domain,
      resource: {
        startDate: '2019-03-01',
        endDate: '2019-03-31'
      }
    }
    

    Reference: