Google Cloud Search Query via node.js Error: Invalid JSON payload received

Google Cloud Search Query via node.js Error: Invalid JSON payload received

googleapis node.js 库在尝试查询云搜索时返回以下错误 API。

Error: Invalid JSON payload received. Unknown name \"requestOptions[searchApplicationId]\": Cannot bind query parameter. Field 'requestOptions[searchApplicationId]' could not be found in request message."

负载与此处记录的完全一样,https://developers.google.com/cloud-search/docs/reference/rest/v1/query/search。 requestOptions[searchApplicationId] 存在,如果我删除它,我会收到一条错误消息,指出需要 searchApplicationId。

代码:

const {google} = require('googleapis');

const service = google.cloudsearch({version: 'v1'});
service.query.search({
  auth: jwtClient,
  requestOptions: {
    searchApplicationId: 'searchapplications/default',
    debugOptions:{enableDebugging: true}
  },
  query: 'My query'
}).then((res) => {
  console.log(JSON.stringify({results:res.results.length}));
  console.log(JSON.stringify({resultsInfo:res.results[0]}));
}).catch((err) => {
  console.error('Unexpected error with cloud search API.');
  console.error(err.toString());
});

查询在 API 浏览器中有效。 https://developers.google.com/apis-explorer/#search/cloudsearch/m/cloudsearch/v1/cloudsearch.query.search?_h=1&resource=%257B%250A++%2522requestOptions%2522%253A+%250A++%257B%250A++++%2522searchApplicationId%2522%253A+%2522searchapplications%252Fdefault%2522%250A++%257D%252C%250A++%2522query%2522%253A+%2522Testing%2522%250A%257D&

我是不是漏掉了一些简单的东西?这是 Google 的客户端库的问题吗? (https://github.com/googleapis/google-api-nodejs-client)如有任何帮助,我们将不胜感激。

终于想通了。必须将请求包装在 requestBody JSON.

service.query.search({
  auth: jwtClient,
  requestBody: {
    requestOptions: {
      searchApplicationId: 'searchapplications/default',
      debugOptions:{enableDebugging: true}
    },
    query: 'My query'
  }
})