Microsoft 认知 API 令牌不起作用
Microsoft cognitive API token doesn't work
我正在尝试使用 Microsoft 认知 API 使用 their documentation 推荐的 curl
方法进行文本分析:
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." }
我也尝试删除 {}
周围的标记和要分析的文本。我在这里做错了什么?
注意:是的,我意识到显示密钥存在安全问题,但我已经重新生成了,谢谢。
您的请求存在三个问题:
Content-Type
header 应该是 application/json
。这可能是 copy-paste 错误。
Ocp-Apim-Subscription-Key
header 值必须是没有 大括号的 API 。这就是您的 401 错误的原因。
- body 必须是特定格式的 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":[]}
我正在尝试使用 Microsoft 认知 API 使用 their documentation 推荐的 curl
方法进行文本分析:
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." }
我也尝试删除 {}
周围的标记和要分析的文本。我在这里做错了什么?
注意:是的,我意识到显示密钥存在安全问题,但我已经重新生成了,谢谢。
您的请求存在三个问题:
Content-Type
header 应该是application/json
。这可能是 copy-paste 错误。Ocp-Apim-Subscription-Key
header 值必须是没有 大括号的 API 。这就是您的 401 错误的原因。- body 必须是特定格式的 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":[]}