0

我在使用 bing 新闻搜索 api 时遇到了一些问题,最奇怪的是按类别搜索,根据文档,类别必须是一个字符串,我发现它接受的值有多个资源,所以我决定使用控制台(https://dev.cognitive.microsoft.com/docs/services/56b43f72cf5ff8098cef380a/operations/56b449fbcf5ff81038d15cdf/console)。一旦您选择了一个类别并将其作为您的密钥,它就可以正常工作。问题是当我将完全相同的 URL 从控制台复制到邮递员时,我使用相同的密钥,并且我得到了带有此正文的“400 Bad Request”标头:

{
  "_type": "ErrorResponse",
  "errors": [
    {
      "code": "RequestParameterInvalidValue",
      "message": "Parameter has invalid value.The category parameter is invalid.",
      "parameter": "category",
      "value": "Entertainment"
    }
  ]
}

知道我在这里做错了什么吗?

4

1 回答 1

0

这是一个非常hacky的解决方法:由于请求通过开发人员控制台正常工作,我检查了请求并尝试重复它认为curl,似乎工作正常,命令如下:

curl 'https://dev.cognitive.microsoft.com/console/query'  -H 'Content-Type: application/json' --data-binary '{"httpMethod":"GET","host":"api.cognitive.microsoft.com","scheme":"https","path":"bing/v5.0/news/?Category=Entertainment","headers":[{"name":"Host","value":"api.cognitive.microsoft.com","inputTypeValue":"text","revealed":false,"options":null,"required":true,"readonly":true,"custom":false},{"name":"Ocp-Apim-Subscription-Key","value":"<your key value>","inputTypeValue":"password","revealed":false,"options":[],"required":true,"readonly":false,"custom":true,"secret":true}],"parameters":[{"name":"Category","value":"Entertainment","inputType":"text","required":false,"options":["Business","Entertainment","Health","Politics","ScienceAndTechnology","Sports","US/UK","World"],"custom":false,"description":"<p>Specifies which category of news articles the caller wants returned.</p>\n","typeName":"string"}],"body":""}'

我发送的 json 数据是这样的(从上面的命令复制)

{ "httpMethod": "GET", "host": "api.cognitive.microsoft.com", "scheme": "https", "path": "bing/v5.0/news/?Category=Entertainment", “headers”:[{ “name”:“Host”,“value”:“api.cognitive.microsoft.com”,“inputTypeValue”:“text”,“revealed”:false,“options”:null,“required ": true, "readonly": true, "custom": false }, { "name": "Ocp-Apim-Subscription-Key", "value": "", "inputTypeValue": "password", "revealed" :假,“选项”:[],“必需”:真,"readonly": false, "custom": true, "secret": true }], "parameters": [{ "name": "Category", "value": "Entertainment", "inputType": "text", “必填”:false,“选项”:[“商业”、“娱乐”、“健康”、“政治”、“科学与技术”、“体育”、“美国/英国”、“世界”]、“自定义”:错误,“描述”:“"inputType": "text", "required": false, "options": ["Business", "Entertainment", "Health", "Politics", "ScienceAndTechnology", "Sports", "US/UK", "世界"], "自定义": false, "描述": ""inputType": "text", "required": false, "options": ["Business", "Entertainment", "Health", "Politics", "ScienceAndTechnology", "Sports", "US/UK", "世界"], "自定义": false, "描述": "

指定调用者希望返回的新闻文章类别。

\n", "typeName": "string" }], "body": "" }

我暂时将其标记为正确,因为我找不到任何其他解决方案

于 2017-04-01T11:45:45.350 回答