Philip Kirkbride
Philip Kirkbride

Reputation: 22879

Microsoft cognitive API token doesn't work

I'm trying to use the Microsoft cognitive API for text analysis using the recommended curl method from their documentation:

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.}" 

But I get back:

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

I also tried removing the {} surrounding token and text to be analyzed. What am I doing wrong here?

enter image description here

Note: yes I realize the security issue with showing key but I have re-generated thanks.

Upvotes: 2

Views: 388

Answers (1)

cthrash
cthrash

Reputation: 2973

There are three issues with your request:

  • Content-Type header should be application/json. This is likely a copy-paste error.
  • Ocp-Apim-Subscription-Key header value must be the API without the curly braces. This is the cause for your 401 error.
  • The body must be JSON of a particular format. You can find the schema here.

Here's the rewritten request:

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."}]}' 

Which should result in:

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

Upvotes: 2

Related Questions