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

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

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

我正在做一切,如下所示:

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

我的问题是:

计算机视觉 API 结果:

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

我不知道出了什么问题。我有合适的关键点和正确的端点。我还检查了这里的所有内容:

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 = "";
        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>");
        });
    }

最佳答案

您必须在 REST API 调用中使用与获取订阅 key 相同的区域

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

第二,您必须找到正确的端点来进行调用。例如,如果我想调用计算机视觉 API,我的位置是美国东部,我将使用 key 1 或 2,然后我将使用以下端点美国东部 - https://eastus。 api.cognitive.microsoft.com/face/v1.0/Detect

您现在可以访问该 API。

更多关于问题排查的详细信息,您可以引用此article还有这个one .

关于c# - Azure 认知服务 - 为什么我无权访问服务? (401错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53218121/

相关文章:

c# - 从 Net Core 3.1 迁移到 Net 6 期间,以下方法或属性之间的调用不明确

c# - 如何在 .NET Core DI 中强制创建类的实例?

c# - SelectList当bindproperty需要的值是asp.net和entity framework中的一个类

c# - 如何将变量值背后的代码绑定(bind)到javascript?

c# - Android 应用程序中的 Process.Start() 访问被拒绝 (0x80004005)

asp.net - 部署后关闭 Azure 应用服务

.net - 在 azure 代理中运行 nunit 测试时找不到 testhost.deps.json 文件

azure - Microsoft Teams 访问策略(即 New-CsApplicationAccessPolicy 命令)存在问题

asp.net - 如何从一个页面处理程序发送到另一个页面处理程序?

c# - 在 C# 中使用句柄和系统观察器