3
votes

I am writing a .Net Core web application, where I use Azure - Computer Vision.

I am doing everything as it is shown here:

https://docs.microsoft.com/pl-pl/azure/cognitive-services/computer-vision/vs-computer-vision-connected-service

and my problem is:

Computer Vision API results:

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

I don't know what is wrong. I have suitable key and right endpoint. I checked also all of the things that are here:

https://blogs.msdn.microsoft.com/kwill/2017/05/17/http-401-access-denied-when-calling-azure-cognitive-services-apis/

Here is my code:

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        // TODO: Change this to your image's path on your site. 
        string imagePath = @"images/family.jpg";

        // Enable static files such as image files. 
        app.UseStaticFiles();

        string visionApiKey = "";
        string visionApiEndPoint = "ComputerVisionAPI_ServiceEndPoint";

        HttpClient client = new HttpClient();

        // Request headers.
        // client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", visionApiKey);
        client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "");

        // Request parameters. A third optional parameter is "details".
        string requestParameters = "visualFeatures=Categories,Description,Color&language=en";

        // Assemble the URI for the REST API Call.
       // string uri = visionApiEndPoint + "/analyze" + "?" + requestParameters;
        string uri = "https://westus.api.cognitive.microsoft.com/vision/v1.0" + "/analyze" + "?" + requestParameters;


        HttpResponseMessage response;

        // Request body. Posts an image you've added to your site's images folder. 
        var fileInfo = env.WebRootFileProvider.GetFileInfo(imagePath);
        byte[] byteData = GetImageAsByteArray(fileInfo.PhysicalPath);

        string contentString = string.Empty;
        using (ByteArrayContent content = new ByteArrayContent(byteData))
        {
            // This example uses content type "application/octet-stream".
            // The other content types you can use are "application/json" and "multipart/form-data".
            content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");

            // Execute the REST API call.
            response = client.PostAsync(uri, content).Result;

            // Get the JSON response.
            contentString = response.Content.ReadAsStringAsync().Result;
        }

        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.Run(async (context) =>
        {
            await context.Response.WriteAsync("<h1>Cognitive Services Demo</h1>");
            await context.Response.WriteAsync($"<p><b>Test Image:</b></p>");
            await context.Response.WriteAsync($"<div><img src=\"" + imagePath + "\" /></div>");
            await context.Response.WriteAsync($"<p><b>Computer Vision API results:</b></p>");
            await context.Response.WriteAsync("<p>");
            await context.Response.WriteAsync(JsonPrettyPrint(contentString));
            await context.Response.WriteAsync("<p>");
        });
    }
1
Where did you create the endpoint to generate the keys? It can be a bit confusing since you can create keys through the cognitive services site and through the Azure portal.Jon
@Zan, please don't post your keys to this or any forum. You should edit you question and roll your keysKen W MSFT
@JoeyCai, thanks for your answer. I've changed my endpoint: westeurope.api.cognitive.microsoft.com/vision/v1.0 imgur.com/b7el0P5 <--- here is my overview. But now i have different error: Computer Vision API results: { "statusCode": 404, "message": "Resource not found" } I checked status and location in properities and everything is correct. What else could be wrong?xzb
@Jon I created my endpoint through the Azure portal.xzb
It seems that you missed the trailing slash and at the end of endpoint. You could refer to this issue.Joey Cai

1 Answers

2
votes

You must use the same region in your REST API call as you used to obtain your subscription keys.

First, you must find the location of your subscription. In order to find the location of your subscription region, you must go to Cognitive Services -> Properties under the Label Location, you will find your subscription region. See below.

Second you must find the correct endpoint to make the call to. For example, if I want to make a call to the Computer Vision API, My location is East US, I will use either key 1 or 2, then I will use the following endpoint East US - https://eastus.api.cognitive.microsoft.com/face/v1.0/detect

You will now be able to have access to the API.

For more details about troubleshoot, you could refer to this article and this one.