Microsoft Cognitive Services - Computer Vision: Invalid Subscription Key

假如想象 提交于 2020-01-03 17:34:39

问题


I'm trying to use the Computer Vision API from Microsoft's Cognitive Services. However, my keys don't seem to be working. I created an account using the free trial of that API and got the two keys from it. Trying to use the key with the ProjectOxford.Vision SDK always yields:

Access denied due to invalid subscription key. Make sure to provide a valid key for an active subscription.

I tried the API console, however I get the same error with my key in the Ocp-Apim-Subscription-Key field. I tried both keys and neither of them work. I even got the free version of Face API and tried its console, but encountered the same issue with its keys. I even tried different datacenters, but they all seem to return the same error.

This would need to be some problem with the key then right? This can't be a problem with my C# code, since the console doesn't work either. And since it's failing in the API console, there's nothing more I can do to rule out any other possibilities is there? I'm not sure what else I can do to debug this. I'd like to regenerate my keys (I saw a tutorial video which showed an older UI of getting the API keys and they used to have a "regenerate" link) but I don't see a way of doing that anymore.

I only just made the account and registered for the APIs, so there's no way I'd be over quota. Is there something else I need to do to enable these keys or something?


回答1:


If you are using the free trial keys got from azure.microsoft.com. You need to change the API endpoint region to westcentralus. The previous default region is westus and I think that is the reason of the invalid key problem.




回答2:


I managed to skirt around the issue of 'Access Denied' by performing the following actions:

  1. I created a free Azure account
  2. I set up an instance of the Cognitive Services Api (this generated a pair of new keys for me to use)
  3. Utilizing the new key, I had to use the following link: https://westus.api.cognitive.microsoft.com/vision/v1.0/ocr

    Instead of

    https://westus.api.cognitive.microsoft.com/vision/v1.0/recognizeText

(I obtained this link from the Cognitive Services Test Dashboard).

Look at the request pattern on the test dashboard and you should be able to tell how to use the api.




回答3:


Even when @Xuan Hu response states correctly to the solution, I scratched my head some time trying to figure out how to change the end point. Here are my 2 cents:

  1. Go to portal.azure.com, in the dashboard of your subscription to the Cognitive Services > General Information > End Point take note of the URL. You need it.

  2. Find in the code of your VisionAPI samples where the VisionServiceClient is instantiated:

    VisionServiceClient VisionServiceCliente = new VisionServiceClient(SubscriptionKey);

and change including the URL that you found in Azure:

VisionServiceClient VisionServiceCliente = new VisionServiceClient(SubscriptionKey, StringOfMyURLTakedFromPortal);

That worked for me.




回答4:


FYI, there is a blog post that covers all of the 401 Access Denied scenarios, including this one regarding the free API keys and region specific API endpoint. Adding it here for folks in the future who find this SO post - https://blogs.msdn.microsoft.com/kwill/2017/05/17/http-401-access-denied-when-calling-azure-cognitive-services-apis/.


Using the incorrect regional endpoint

Most of the Cognitive Services APIs are region specific, which means that during API account creation you select which region you want to create the account in. These APIs have region specific endpoints such as westus.api.cognitive.microsoft.com or eastus2.api.cognitive.microsoft.com, and an API key for an account created in one region will only work using the endpoint for that specific region. This means that if you create an API account in West US you will not be able to call the eastus2.api.cognitive.microsoft.com API endpoint.

You can verify the region and endpoint in the Azure management portal.

Trial API Keys

The free trial API keys have 30 day expiration dates, and the same restrictions for region and version. If you are using the trial keys you can go to https://azure.microsoft.com/en-us/try/cognitive-services/my-apis/ to manage your API keys (if you are not already logged in then just click one of the ‘Create’ buttons and you can go through the wizard to login and see your existing API keys), and you will also see the expiration date and endpoint.



来源:https://stackoverflow.com/questions/44094186/microsoft-cognitive-services-computer-vision-invalid-subscription-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!