我正在使用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'
是可选的。
面对类似的问题,在uri中添加“/ detect”之后问题就解决了。见下文
var uri = "https://westus.api.cognitive.microsoft.com/face/v1.0/detect
还要确保订阅密钥有效。