Microsoft认知API令牌不起作用

时间:2017-02-16 02:44:53

标签: microsoft-cognitive

我尝试使用来自their documentation的推荐curl方法使用Microsoft认知API进行文本分析:

curl -v -X POST "https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/sentiment" -H "Content-Type: application/jscp-Apim-Subscription-Key: {bc94cba9b84748ebb2f2b79a28ee3450}" --data-ascii "{I had a wonderful experience! The rooms were wonderful and the staff were helpful.}" 

但我回来了:

{ "statusCode": 401, "message": "Access denied due to invalid subscription key. Make sure to provide a valid key for an active subscription." }

我还尝试删除要分析的{}周围令牌和文本。我在这里做错了什么?

enter image description here

注意:是的,我通过显示密钥来解决安全问题,但我已经重新生成了感谢。

1 个答案:

答案 0 :(得分:2)

您的请求有三个问题:

  • Content-Type标题应为application/json。这可能是复制粘贴错误。
  • Ocp-Apim-Subscription-Key标头值必须是API ,不带花括号。这是导致401错误的原因。
  • 正文必须是特定格式的JSON。您可以找到架构here

这是重写的请求:

curl -v "https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/sentiment" -H "Content-Type: application/json" -H "Ocp-Apim-Subscription-Key: $OXFORD_TEXT_KEY" --data-ascii '{"documents":[{"language":"en","id":"1234","text":"I had a wonderful experience! The rooms were wonderful and the staff were helpful."}]}' 

哪个应该导致:

{"documents":[{"score":0.9750894,"id":"1234"}],"errors":[]}