【问题标题】:Google Search Console API, google.webmasters.searchanalytics.query "startDate field is required"谷歌搜索控制台 API,google.webmasters.searchanalytics.query“startDate 字段是必需的”
【发布时间】:2019-08-23 22:29:22
【问题描述】:

我正在尝试使用 Google Search Console API (nodejs) 来检索查询报告。我们拥有一个配置了我公司所有域的 google 帐户。我们想从 api 中检索完整的域列表,然后从每个域中获取数据。

我们能够正确获取完整的域列表。但是我们可以得到它们的任何数据。

这是一个简短的代码示例。

// 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

但我总是收到以下错误。这确实是不言自明的。但我无法理解它,因为我在 p 对象中提供了 startDate 和 endDate 参数。我尝试过不同的日期格式,单引号、双引号、无引号……无论我更改什么,我总是收到必填字段错误。

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

我可以在 Google Search API 控制台中看到错误,因此我认为错误来自服务器,而不是来自我的代码中的某些内容。

通过 API Explorer,我可以毫无错误地测试 api。

我不知道会是什么,但似乎很傻。

【问题讨论】:

    标签: node.js google-api google-search-console google-api-nodejs-client google-api-webmasters


    【解决方案1】:

    这个修改怎么样?

    在 Node.js 的 googleapis 中,请求正文放在 resource 中。因此,在您的情况下,startDateendDate 被放入 resource

    发件人:

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

    收件人:

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

    参考:

    【讨论】:

      【解决方案2】:

      对于从事此工作的任何人,我认为 Google 更改了 API,现在他们使用 requestBody 而不是 resource 因此格式为:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多