我正在使用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'
是可选的。
答案 0 :(得分:5)
在uri中添加“ / detect”后,解决了类似的问题,此问题已解决。 见下文
var uri = "https://westus.api.cognitive.microsoft.com/face/v1.0/detect
还要确保订阅密钥有效。
答案 1 :(得分:3)
您的请求网址必须指定一个组ID,而不是{personGroupId}
。根据{{3}},组ID必须是:
用户提供的personGroupId为字符串。有效字符包括 数字,小写英文字母,' - '和'_'。最大值 personGroupId的长度为64。
此外,http动词需要PUT,而你已经发出了client.PostAsync
请求。因此,您需要将其更改为client.PutAsync
。
Microsoft为spec的C#提供了一个客户端库,您可以在其中找到可用的C#代码。
答案 2 :(得分:0)
在python中,这确实对我有用。
ENDPOINT ='https://westcentralus.api.cognitive.microsoft.com'