1

I am trying to make a simple image analysis project using AI-tools for Visual Studio. I have created an Azure Cognitive Service as a ComputerVision service. It has been more than 10 minutes since I created it so the api keys should be valid. Then I created a new project by right clicking on it. But when I run it then I get this exception:

 Please input image url or locate a local image file. If input is empty, example image will be used.
No url or file specified, use the example https://oxfordportal.blob.core.windows.net/vision/Analysis/1-1.jpg
One or more errors occurred.
 at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
 at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
 at System.Threading.Tasks.Task`1.get_Result()
 at GetImageDetailsService.Program.Main(String[] args) in C:\Users\X\Documents\Visual Studio 2017\Projects\CognitiveServiceTest\GetImageDetailsService\Program.cs:line 27
Microsoft.Azure.CognitiveServices.Vision.ComputerVision.Models.ComputerVisionErrorException: Operation returned an invalid status code 'Unauthorized'
 at Microsoft.Azure.CognitiveServices.Vision.ComputerVision.ComputerVisionAPI.<DescribeImageWithHttpMessagesAsync>d__30.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
 at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
 at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
 at Microsoft.Azure.CognitiveServices.Vision.ComputerVision.ComputerVisionAPIExtensions.<DescribeImageAsync>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
 at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
 at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
 at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
 at Microsoft.CognitiveServices.Samples.ComputerVisionApiExtensions.<DescribeImage>d__2.MoveNext() in C:\Users\X\Documents\Visual Studio 2017\Projects\CognitiveServiceTest\GetImageDetailsService\ComputerVisionApiExtensions.cs:line 34

Apparently I am not authorized. What can be causing this? I am logged into my Azure subscription and I have funds (I am using F0 tier). I have also checked that the region is correct and that the api key matches.

Edit: When I change the api key to something different in the application call then I still get an unauthorized exception.

Edit2: Added code that calls the api.

var client = new ComputerVisionApiExtensions(ComputerVisionSubscriptionKey, ComputerVisionRegion, null);
var result = client.DescribeImage(imagePath).Result;
asked May 16, 2018 at 7:13
4
  • Can you put the code where you call the API? Commented May 16, 2018 at 9:59
  • Done. This code is generated using the Visual Studio AI Tools. Commented May 16, 2018 at 11:09
  • Thanks! Did you create the API key through the Azure Portal or through the Cognitive Services website? Commented May 16, 2018 at 14:27
  • I created it through Visual Studio AI Tools in the Server Explorer. In the "Azure Cognitive Services" I right clicked and chose the "Create New Cognitive Service". I can see this service in my Azure Portal after I created it. The API key is also identical to the one the application automatically chose to use when I created the application. Commented May 17, 2018 at 7:39

2 Answers 2

1

I was getting the same thing and thought it would be worthwhile to submit an issue. I got a response for a fix, tested it, and it all worked!

To solve the issue, just put this.AzureRegion = GetRegion(region); inside the constructor in the ComputerVisionApiExtensions.cs file.

answered May 24, 2018 at 9:28
Sign up to request clarification or add additional context in comments.

2 Comments

YES! Thank you. I knew it was something weird like this and possibly a bug. I had tried everything to try to get it to work and had confirmed that everything in Azure was working. :D Thanks! Accepted as the answer.
Glad it could help!
1

For the Computer Vision API type of Azure Cognitive Services, the request for analyzing an image would look like as follows:

POST https://{location-of-your-Computer-Vision-API}.api.cognitive.microsoft.com/vision/v1.0/analyze?visualFeatures=Categories&language=en
Host: {location-of-your-Computer-Vision-API}.api.cognitive.microsoft.com
Content-Type: application/json
Ocp-Apim-Subscription-Key: {subscription-key-under-the-same-region-of-your-Computer-Vision-API}
{"url":"https://oxfordportal.blob.core.windows.net/vision/Analysis/1-1.jpg"}

Note: You must use the same region in your REST call as you used to get your subscription keys. For example, if you got your subscription keys from westus, replace "westcentralus" in the URL below with "westus".

If your Azure Cognitive Services is under East Asia, while you use the subscription key to request westcentralus.api.cognitive.microsoft.com instead of eastasia.api.cognitive.microsoft.com, then you could get 401 Access Denied and the following response:

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

You could use fiddler to capture the network traces when running your application to narrow this issue. Also, you could follow the detailed tutorials under "RESOURCE MANAGEMENT> Quick start" of your Cognitive Services via Azure Portal.

answered May 21, 2018 at 3:00

2 Comments

Creating a project in Visual Studio from the AI tools does all this for you. It sets the region to whatever you have specified in the portal as well as setting the api key. That is why it is so weird that it fails. I have also double checked that these values are correct and they are. I can also use the Computer Vision API reference with my key and it works fine. The only thing that does not work is calling it from code for some reason. I am baffled.
I just used postman and created a console by using the HttpClient to send the request. Though, you have check the related settings, I would also recommend you use fiddler to capture the network traces when running your code to narrow this issue. My similar issue caused by the wrong region name.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.