阿里云人脸属性API,我已经成功了(高兴高兴)
想學習一下阿里的人臉識別API,Java版的參照https://help.aliyun.com/document_detail/67818.html?spm=a2c4g.11186623.6.559.a31e4bd9BDsnO9 已經(jīng)沒有問題,成功了。當我把這些代碼轉(zhuǎn)換為C#時,說死是不成功啊。
Java版中只是把其中的main函數(shù)改了一下,如下:
當我使用C#來實現(xiàn)時,打死不成功啊。
Main函數(shù)如下:
其中的DoPost函數(shù)如下:
public static string DoPost(string url, string body, string ak_id, string ak_secret){ string result = string.Empty;HttpClient httpClient = new HttpClient();httpClient.MaxResponseContentBufferSize = 256000;httpClient.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36");DateTime dateNow = DateTime.Now;string bodyMd5 = MD5Base64(body);string method = "POST";string accept = "application/json";string content_type = "application/json";string date = ToGMTstring(dateNow);//HttpContent httpContent = new StringContent(bodyMd5);//此處一定要用ByteArrayContent,用二進制數(shù)組發(fā)送數(shù)據(jù)組API。HttpContent httpContent = new ByteArrayContent(Encoding.UTF8.GetBytes(body));httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");//設置Http的內(nèi)容標頭的字符//httpContent.Headers.ContentType.CharSet = "utf-8";httpClient.DefaultRequestHeaders.Date = dateNow;httpClient.DefaultRequestHeaders.Accept.Clear();httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(accept));string stringToSign = method + "\n" + accept + "\n" + bodyMd5 + "\n" + content_type + "\n" + date + "\n"+ "/face/attribute";// 2.計算 HMAC-SHA1string signature = HMACSha1(stringToSign, ak_secret);httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Dataplus", ak_id + ":" + signature);HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result;Console.WriteLine(response.StatusCode);result = response.Content.ReadAsStringAsync().Result;return result;}引用的計算MD5和SHA的函數(shù)轉(zhuǎn)換為C#如下:
public static string ToGMTstring(DateTime date){return date.ToUniversalTime().ToString("r");}/// 計算MD5+BASE64public static string MD5Base64(string s){if (s == null)return null;string encodeStr = "";byte[] utfBytes = System.Text.Encoding.UTF8.GetBytes(s);MD5 md5 = new MD5CryptoServiceProvider();try{byte[] md5Bytes = md5.ComputeHash(utfBytes);encodeStr = Convert.ToBase64String(md5Bytes);}catch (Exception e){throw new Exception("Failed to generate MD5 : " + e.Message);}return encodeStr;}/// 計算 HMAC-SHA1public static string HMACSha1(string data, string key){try{HMACSHA1 myHMACSHA1 = new HMACSHA1(Encoding.UTF8.GetBytes(key));byte[] byteText = myHMACSHA1.ComputeHash(Encoding.UTF8.GetBytes(data));return System.Convert.ToBase64String(byteText);}catch (Exception e){throw new Exception("Failed to generate HMAC : " + e.Message);}}很郁悶的就是調(diào)用不成功,取得的響應內(nèi)容是
response body:The authorization(Dataplus my_ak_id:my_signature) of this request Invalid! {"url":"/face/attribute","headers":{"date":"Wed, 24 Oct 2018 05:50:10 GMT","accept":"application/json","authorization":"Dataplus my_ak_id:my_signature","content-type":"application/json"},"method":"POST"}
{"url":"/face/attribute","headers":{"date":"Wed, 24 Oct 2018 05:50:10 GMT","accept":"application/json","authorization":"Dataplus my_ak_id:my_signature","content-type":"application/json"},"method":"POST"}
其中輸出已做替換my_ak_id:my_signature實際輸出是我的ak_id和計算的簽名。
在大佬在嗎?指導一下。
總結
以上是生活随笔為你收集整理的阿里云人脸属性API,我已经成功了(高兴高兴)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: DataWorks 如何设置调度依赖
- 下一篇: (转)K-近邻算法(KNN)