Azure认知服务-为什么我无法访问服务? (401错误)

时间:2018-11-09 00:25:10

标签: c# azure asp.net-core computer-vision azure-cognitive-services

我正在编写一个使用Azure - Computer Vision的.Net Core Web应用程序。

我正在做这里显示的所有事情:

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

我的问题是:

Computer Vision API结果:

  

{“ statusCode”:401,“ message”:“由于无效的订阅密钥而拒绝访问。请确保为有效的订阅提供有效的密钥。 }

我不知道怎么了。我有合适的关键和正确的终点。我还检查了这里所有的东西:

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

这是我的代码:

    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 = "71a481e3473440d18c586a038365bd79";
        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", "71a481e3473440d18c586a038365bd79");

        // 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 个答案:

答案 0 :(得分:1)

您必须在用于获取订阅密钥的REST API调用中使用相同区域

首先,您必须找到订阅的位置。为了找到您的订阅区域的位置,您必须转到“认知服务”->“标签位置”下的“属性”,您将找到您的订阅区域。见下文。

第二个您必须找到正确的端点才能进行呼叫。例如,如果我想调用Computer Vision API,我的位置是美国东部,则将使用键1或2,然后将使用以下端点美国东部-https://eastus.api.cognitive.microsoft.com/face/v1.0/detect

您现在将可以访问API。

有关故障排除的更多详细信息,您可以参考此article和此one