Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Azure认知服务-为什么不';我不能使用服务吗?(401错误)_C#_Azure_Asp.net Core_Computer Vision_Azure Cognitive Services - Fatal编程技术网

C# Azure认知服务-为什么不';我不能使用服务吗?(401错误)

C# Azure认知服务-为什么不';我不能使用服务吗?(401错误),c#,azure,asp.net-core,computer-vision,azure-cognitive-services,C#,Azure,Asp.net Core,Computer Vision,Azure Cognitive Services,我正在编写一个.Net核心web应用程序,我使用它 我所做的一切都如图所示: 我的问题是: 计算机视觉API结果: {“statusCode”:401,“message”:“由于订阅密钥无效,访问被拒绝。请确保为活动订阅提供有效密钥。”} 我不知道怎么了。我有合适的钥匙和右端点。我还检查了这里的所有内容: 这是我的密码: public void Configure(IApplicationBuilder app, IHostingEnvironment env) {

我正在编写一个.Net核心web应用程序,我使用它

我所做的一切都如图所示:

我的问题是:

计算机视觉API结果:

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

我不知道怎么了。我有合适的钥匙和右端点。我还检查了这里的所有内容:

这是我的密码:

    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>");
        });
    }
public void配置(IApplicationBuilder应用程序,IHostingEnvironment环境)
{
//TODO:将此更改为站点上图像的路径。
字符串imagePath=@“images/family.jpg”;
//启用静态文件,如图像文件。
app.UseStaticFiles();
字符串visionApiKey=“”;
字符串visionApiEndPoint=“ComputerVisionAPI\u ServiceEndPoint”;
HttpClient=新的HttpClient();
//请求头。
//client.DefaultRequestHeaders.Add(“Ocp Apim订阅密钥”,visionApiKey);
client.DefaultRequestHeaders.Add(“Ocp Apim订阅密钥”,”);
//请求参数。第三个可选参数是“details”。
string requestParameters=“visualFeatures=Categories,Description,Color&language=en”;
//组装RESTAPI调用的URI。
//字符串uri=visionApiEndPoint+“/analyze”+“?”+requestParameters;
字符串uri=”https://westus.api.cognitive.microsoft.com/vision/v1.0“+”/分析“+”?“+请求参数;
HttpResponseMessage响应;
//请求正文。将已添加的图像发布到站点的“图像”文件夹。
var fileInfo=env.WebRootFileProvider.GetFileInfo(imagePath);
字节[]byteData=GetImageAsByteArray(fileInfo.PhysicalPath);
string contentString=string.Empty;
使用(ByteArrayContent content=新的ByteArrayContent(byteData))
{
//本例使用内容类型“应用程序/八位字节流”。
//您可以使用的其他内容类型有“application/json”和“multipart/formdata”。
content.Headers.ContentType=新的MediaTypeHeaderValue(“应用程序/八位字节流”);
//执行RESTAPI调用。
response=client.PostAsync(uri,content.Result);
//获取JSON响应。
contentString=response.Content.ReadAsStringAsync().Result;
}
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.Run(异步(上下文)=>
{
wait context.Response.WriteAsync(“认知服务演示”);
wait context.Response.WriteAsync($“测试图像:

”); wait context.Response.WriteAsync($“”); wait context.Response.WriteAsync($“计算机视觉API结果:

”); wait context.Response.WriteAsync(“”); wait context.Response.WriteAsync(JsonPrettyPrint(contentString)); wait context.Response.WriteAsync(“”); }); }
您必须在REST API调用中使用与获取订阅密钥相同的区域

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

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

您现在可以访问API了


有关疑难解答的更多详细信息,请参阅此项和此项。

您在何处创建用于生成密钥的端点?这可能有点令人困惑,因为您可以通过认知服务网站和Azure门户创建密钥。@Zan,请不要将密钥发布到此或任何论坛。你应该编辑你的问题并滚动你的答案keys@JoeyCai,谢谢你的回答。我已更改了我的端点:@Jon我通过Azure门户创建了我的端点。似乎您错过了尾随斜杠和端点的末尾。你可以参考这个。正确设置区域对我来说很有效。C#样本在我将区域从“westus”改为“global”之前对我不起作用。对不起,这不是我的问题,所以我不能接受它作为答案。