我正在尝试使用Google Search Console API(nodejs)来检索查询报告。我们拥有一个谷歌帐户,其中配置了我公司的所有域。我们希望从 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 资源管理器中,我可以测试没有错误的 api。
我不知道它能是什么,但它似乎很愚蠢。
这个修改怎么样?
在 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'
}
}
参考:
- 搜索分析:查询
对于任何正在研究这个问题的人,我认为谷歌改变了API,现在他们使用requestBody而不是资源,因此格式为:
p = {
auth: auth,
siteUrl: domain,
requestBody: {
startDate: '2020-03-01',
endDate: '2020-03-31'
}
}