简体   繁体   中英

Accessing Microsoft Cognitive Service through Restful Service

I'm receiving the following error while accessing the Microsoft Cognitive API:

在此处输入图片说明

I'm 100% sure that my subscription key is valid because I have tested it in DHC as well as the online tool of Project Oxford.

I'm using sample code provided by Microsoft. Here it is...

var client = new HttpClient();
var queryString = HttpUtility.ParseQueryString("safeee");

client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "{bce8988422e94fd3ac64xxxxxxxxxxxx}");

var uri = "https://api.projectoxford.ai/face/v1.0/persongroups/{personGroupId}?" + queryString;

HttpResponseMessage response;

byte[] byteData = Encoding.UTF8.GetBytes("{body}");

using (var content = new ByteArrayContent(byteData))
{
    content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
    response = await client.PutAsync(uri, content);

    MessageBox.Show(response.ToString());
}

For any code snippet in the Microsoft Cognitive Services site, including the page to which I believe you're referring, you need to substitute all curly-braced strings with appropriate values. In your case, you need to:

  1. Drop the query string "safeee" from the queryString . There are no query parameters for this particular endpoint.
  2. Drop the curly braces in the Ocp-Apim-Subscription-Key value (sounds like you tried that.)
  3. Give an approriate personGroupId value. Per the documentation on the aforementioned page, "valid characters include numbers, English letters in lower case, '-' and '_'. The maximum length of the personGroupId is 64."
  4. Provide a proper JSON value for body . In your case you might simply use "{\\"name\\":\\"safeee\\"}" .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM