找不到Microsoft Face API 1.0错误资源

问题描述 投票:2回答:2

我正在使用Microsoft Azure Cognitive服务开展面部识别项目。不太确定为什么我无法纠正我自己的JSON格式错误的语法,我认为我在6个月之前就已经开始了。我想创建一个组名,所以我调用'Person Group API',每次我按照MS示例我的代码都会出错,但是在API测试控制台中没有问题,我的代码示例是从MS站点借用的:

 { "error": { "code": "ResourceNotFound", "message": "The requested resource was not found." } }

以及在控制台模式下运行的代码:

static async void CreateGroup()
        {
            string key1 = "YourKey"; 
             // azure the one should work 
            var client = new HttpClient();
            var queryString = HttpUtility.ParseQueryString(string.Empty);

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

        var uri = "https://westus.api.cognitive.microsoft.com/face/v1.0/
        persongroups/{personGroupId}?" + queryString;

        HttpResponseMessage response;

        // Request body
        string groupname = "myfriends";

        string body = "{\"name\":\"" + groupname + ","+ "\"}";
        // Request body
        using (var content = new StringContent
        (body, Encoding.UTF8, "application/json"))
        {

            await client.PostAsync(uri, content)
                .ContinueWith(async responseTask =>
                {
                    var responseBody = await responseTask.Result
                    .Content.ReadAsStringAsync();
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine("Response: {0}", responseBody);
                    Console.WriteLine("");

                    Console.WriteLine("Group Created.... ");
                    Console.WriteLine("Hit ENTER to exit...");
                    Console.ReadKey();
                });

            response = await client.PutAsync(uri, content);
            Console.WriteLine("what is this {0}", response.ToString());
            Console.ReadKey();

        }// end of using statement 


    }// end of CreateGroup
    #endregion

我猜在这里,但我认为我的JSON再次出错,我不知道这次我做错了什么。根据网站我需要发送到ms的字段名称是'name' : 'userData'是可选的。

azure microsoft-cognitive
2个回答
5
投票

面对类似的问题,在uri中添加“/ detect”之后问题就解决了。见下文

var uri = "https://westus.api.cognitive.microsoft.com/face/v1.0/detect

还要确保订阅密钥有效。


3
投票

您的请求网址必须指定一个组ID来代替您拥有{personGroupId}的位置。根据spec,组ID必须是:

用户提供的personGroupId作为字符串。有效字符包括数字,小写英文字母,' - '和'_'。 personGroupId的最大长度为64。

此外,http动词需要PUT,而你已经做了client.PostAsync请求。因此,您需要将其更改为client.PutAsync

Microsoft为Face API提供了C#的客户端库,您可以在其中找到可用的C#代码。

© www.soinside.com 2019 - 2024. All rights reserved.