【问题标题】:400 error with bing news search api必应新闻搜索 api 出现 400 错误
【发布时间】:2017-03-31 18:12:31
【问题描述】:

我在使用 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"
    }
  ]
}

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

【问题讨论】:

    标签: bing bing-search bing-news-search-api


    【解决方案1】:

    这是一个非常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", “方案”:“https”,“路径”:“bing/v5.0/news/?Category=Entertainment”, “标题”:[{“名称”:“主机”,“值”: “api.cognitive.microsoft.com”,“inputTypeValue”:“文本”, “显示”:假,“选项”:空,“必需”:真, “只读”:真,“自定义”:假},{“名称”: "Ocp-Apim-Subscription-Key", "值": "", “inputTypeValue”:“密码”,“显示”:假,“选项”:[], “必需”:真,“只读”:假,“自定义”:真,“秘密”: true }], "参数": [{ "name": "Category", "value": “娱乐”,“inputType”:“文本”,“必需”:false, “选项”:[“商业”,“娱乐”,“健康”,“政治”, “ScienceAndTechnology”、“Sports”、“US/UK”、“World”]、“custom”: 假,“描述”:“

    指定新闻文章的类别 调用者想要返回。

    \n", "typeName": "string" }], “身体”: ”” }

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

    【讨论】: