我正在尝试使用 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." }
我还尝试删除要分析的标记和文本周围的 {}
。我在这里做错了什么?
注意:是的,我意识到显示 key 存在安全问题,但我已重新生成,谢谢。
最佳答案
您的请求存在三个问题:
Content-Type
header 应为application/json
。这可能是复制粘贴错误。Ocp-Apim-Subscription-Key
header 值必须是不带大括号的 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":[]}
关于azure-cognitive-services - Microsoft 认知 API token 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42263899/