2

我正在尝试使用 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。

我不知道它会是什么,但它似乎是非常愚蠢的东西。

4

2 回答 2

1

这个改装怎么样?

在 Node.js 的 googleapis 中,请求正文放在resource. 所以在你的情况下,startDate并且endDate被放入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'
  }
}

参考:

于 2019-04-02T22:28:56.787 回答
1

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

p = {
 auth: auth,
 siteUrl: domain,
 requestBody: {
   startDate: '2020-03-01',
   endDate: '2020-03-31'
  }
}
于 2020-11-19T12:09:14.820 回答