群里有人弄了一个数字人模型,我怀着好奇心去看看。因为之前在抖音上看了很多数字人视频,但是不知道怎么弄的,反正觉得非常牛X的。他说很简单,于是我打开他们的网站,并注册了账号,送了1000积分。想玩玩,怎么使用。我把我的体验写出来吧。
第一步 先注册
这个不用说了。
第二步 选择应用
第三步 生成密钥
第四步 对接api接口
他们有php调用demo,但是我是使用C#的,所以我按照文案对接了几个接口。
C# 代码如下
请求接口
一 请求方法
[RoutePrefix("demo")] public class EHuManController : ApiController { public string secretKey = "你的密钥XXXX"; /// <summary> /// 数字人合成 /// </summary> /// <param name="model"></param> /// <returns></returns> [Route("Create")] [HttpPost] public IHttpActionResult Create([FromBody] CreatedModel model) { string postUrl = "https://api.yidevs.com/app/human/human/Index/created"; string content = JsonConvert.SerializeObject(model); var headers = new Dictionary<string, string>(); headers.Add("Authorization", string.Format("Bearer {0}", secretKey)); var response = HttpHelperService.HttpPost22(postUrl, content, "application/json", headers, 30000); string responseStr = response.Content.ReadAsStringAsync().Result; dynamic respnonModel; if (response != null && response.StatusCode == HttpStatusCode.BadRequest) { respnonModel = JsonConvert.DeserializeObject<ResCreatedModel>(responseStr); } else { respnonModel = JsonConvert.DeserializeObject<ResCreatedModel>(responseStr); } return Json(new { reqData = model,//请求参数 resData = respnonModel,//响应结果 responseStr = responseStr//响应参数 }); } /// <summary> /// 数字人克隆 /// </summary> /// <param name="model"></param> /// <returns></returns> [Route("Scene")] [HttpPost] public IHttpActionResult Scene([FromBody] SceneModel model) { string postUrl = "https://api.yidevs.com/app/human/human/Scene/created"; string content = JsonConvert.SerializeObject(model); var headers = new Dictionary<string, string>(); headers.Add("Authorization", string.Format("Bearer {0}", secretKey)); var response = HttpHelperService.HttpPost22(postUrl, content, "application/json", headers, 30000); string responseStr = response.Content.ReadAsStringAsync().Result; dynamic respnonModel; if (response != null && response.StatusCode == HttpStatusCode.BadRequest) { respnonModel = JsonConvert.DeserializeObject<ResSceneModel>(responseStr); } else { respnonModel = JsonConvert.DeserializeObject<ResSceneModel>(responseStr); } return Json(new { reqData = model,//请求参数 resData = respnonModel,//响应结果 responseStr = responseStr//响应参数 }); } [Route("Chat")] [HttpPost] public IHttpActionResult Chat([FromBody] ChatModel model) { string postUrl = "https://api.yidevs.com/app/human/human/Chat/generate"; string content = JsonConvert.SerializeObject(model); var headers = new Dictionary<string, string>(); headers.Add("Authorization", string.Format("Bearer {0}", secretKey)); var response = HttpHelperService.HttpPost22(postUrl, content, "application/json", headers, 30000); string responseStr = response.Content.ReadAsStringAsync().Result; dynamic respnonModel; if (response != null && response.StatusCode == HttpStatusCode.BadRequest) { respnonModel = JsonConvert.DeserializeObject<ResChatModel>(responseStr); } else { respnonModel = JsonConvert.DeserializeObject<ResChatModel>(responseStr); } return Json(new { reqData = model,//请求参数 resData = respnonModel,//响应结果 responseStr = responseStr//响应参数 }); } }
二请求方法代码
public static class HttpHelperService { /// <summary> /// 异步方法 /// </summary> /// <param name="url"></param> /// <param name="Timeout"></param> /// <param name="method"></param> /// <returns></returns> public static async Task<string> SendHttpRequestAsync(string url, string Body = "", string contentType = null, Dictionary<string, string> headers = null, int Timeout = 30) { byte[] sendData = Encoding.UTF8.GetBytes(Body); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.Timeout = Timeout; // 设置超时时间 if (contentType != null) { request.ContentType = contentType; } if (headers != null) { foreach (var header in headers) { request.Headers.Add(header.Key, header.Value); } } using (Stream sendStream = request.GetRequestStream()) { sendStream.Write(sendData, 0, sendData.Length); sendStream.Close(); } using (HttpWebResponse response = await request.GetResponseAsync() as HttpWebResponse) { using (Stream stream = response.GetResponseStream()) { StreamReader reader = new StreamReader(stream, Encoding.UTF8); return await reader.ReadToEndAsync(); } } } /// <summary> /// 同步方法 /// </summary> /// <param name="url"></param> /// <param name="Timeout"></param> /// <param name="method"></param> /// <returns></returns> public static string SendHttpRequest2(string url, string Body = "", string contentType = null, Dictionary<string, string> headers = null, int Timeout = 30) { byte[] sendData = Encoding.UTF8.GetBytes(Body); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.Timeout = Timeout; // 设置超时时间 if (contentType != null) { request.ContentType = contentType; } if (headers != null) { foreach (var header in headers) { request.Headers.Add(header.Key, header.Value); } } // request.Headers.Add("app_id", "NTEST"); // request.Headers.Add("app_key", "eef7b688-19c4-433b-94f1-300523964f2f"); using (Stream sendStream = request.GetRequestStream()) { sendStream.Write(sendData, 0, sendData.Length); sendStream.Close(); } using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { using (Stream stream = response.GetResponseStream()) { StreamReader reader = new StreamReader(stream, Encoding.UTF8); return reader.ReadToEnd(); } } } public static HttpWebResponse SendHttpRequest(string url, string Body = "", string contentType = null, Dictionary<string, string> headers = null, int Timeout = 3000) { byte[] sendData = Encoding.UTF8.GetBytes(Body); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.Timeout = Timeout; // 设置超时时间 if (contentType != null) { request.ContentType = contentType; } if (headers != null) { foreach (var header in headers) { request.Headers.Add(header.Key, header.Value); } } using (Stream sendStream = request.GetRequestStream()) { sendStream.Write(sendData, 0, sendData.Length); sendStream.Close(); } HttpWebResponse response = (HttpWebResponse)request.GetResponse(); return response; } public static string SendHttpRequest3(string url, string Body = "", string contentType = "application/x-www-form-urlencoded", int Timeout = 3000) { byte[] sendData = Encoding.UTF8.GetBytes(HttpUtility.UrlEncode(Body));//加了一层编码 // byte[] sendData = Encoding.UTF8.GetBytes(Body);//加了一层编码 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.Timeout = Timeout; // 设置超时时间 if (contentType != null) { request.ContentType = contentType; } using (Stream sendStream = request.GetRequestStream()) { sendStream.Write(sendData, 0, sendData.Length); sendStream.Close(); } using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { using (Stream stream = response.GetResponseStream()) { StreamReader reader = new StreamReader(stream, Encoding.UTF8); return reader.ReadToEnd(); } } } /// <summary> /// 设置证书策略 /// </summary> public static void SetCertificatePolicy() { System.Net.ServicePointManager.ServerCertificateValidationCallback += RemoteCertificateValidate; } /// <summary> /// Remotes the certificate validate. /// </summary> private static bool RemoteCertificateValidate( object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors error) { return true; } public static string HttpPost(string url, string body = null, string contentType = null, int timeOut = 3000) { // body = body ?? ""; SetCertificatePolicy(); ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; using (HttpClient client = new HttpClient()) { client.Timeout = new TimeSpan(0, 0, timeOut); using (HttpContent httpContent = new StringContent(UrlEncodeToJava(body), Encoding.UTF8)) { if (contentType != null) httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType); HttpResponseMessage response = client.PostAsync(url, httpContent).Result; return response.Content.ReadAsStringAsync().Result; } } } public static string HttpPost2(string url, string body = null, string contentType = null, Dictionary<string, string> headers = null, int timeOut = 30) { body = body ?? ""; SetCertificatePolicy(); ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; using (HttpClient client = new HttpClient()) { client.Timeout = new TimeSpan(0, 0, timeOut); if (headers != null) { foreach (var header in headers) client.DefaultRequestHeaders.Add(header.Key, header.Value); } using (HttpContent httpContent = new StringContent(body, Encoding.UTF8)) { if (contentType != null) httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType); HttpResponseMessage response = client.PostAsync(url, httpContent).Result; return response.Content.ReadAsStringAsync().Result; } } } public static HttpResponseMessage HttpPost22(string url, string body = null, string contentType = null, Dictionary<string, string> headers = null, int timeOut = 30) { body = body ?? ""; SetCertificatePolicy(); ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; using (HttpClient client = new HttpClient()) { client.Timeout = new TimeSpan(0, 0, timeOut); if (headers != null) { foreach (var header in headers) client.DefaultRequestHeaders.Add(header.Key, header.Value); } using (HttpContent httpContent = new StringContent(body, Encoding.UTF8)) { if (contentType != null) httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType); HttpResponseMessage response = client.PostAsync(url, httpContent).Result; return response; } } } /// <summary> /// 带Bearer token 参数 /// </summary> /// <param name="url"></param> /// <param name="body"></param> /// <param name="contentType"></param> /// <param name="headers"></param> /// <param name="timeOut"></param> /// <param name="accessToken"></param> /// <returns></returns> public static string HttpPost3(string url, string body = null, string contentType = null, Dictionary<string, string> headers = null, int timeOut = 30, string accessToken = "") { body = body ?? ""; SetCertificatePolicy(); ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; using (HttpClient client = new HttpClient()) { client.Timeout = new TimeSpan(0, 0, timeOut); if (headers != null) { foreach (var header in headers) { client.DefaultRequestHeaders.Add(header.Key, header.Value); } client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); } using (HttpContent httpContent = new StringContent(body, Encoding.UTF8)) { if (contentType != null) httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType); HttpResponseMessage response = client.PostAsync(url, httpContent).Result; return response.Content.ReadAsStringAsync().Result; } } } public static HttpResponseMessage HttpPost4(string url, string body = null, string contentType = null, Dictionary<string, string> headers = null, int timeOut = 30, string accessToken = "") { body = body ?? ""; SetCertificatePolicy(); ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; using (HttpClient client = new HttpClient()) { client.Timeout = new TimeSpan(0, 0, timeOut); if (headers != null) { foreach (var header in headers) { client.DefaultRequestHeaders.Add(header.Key, header.Value); } //client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); } if (!string.IsNullOrEmpty(accessToken)) { client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); } using (HttpContent httpContent = new StringContent(body, Encoding.UTF8)) { if (contentType != null) httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType); HttpResponseMessage response = client.PostAsync(url, httpContent).Result; return response; } } } public static string HttpPostEInvoice(string url, string body = null, string contentType = null, Dictionary<string, string> headers = null, int timeOut = 30, string accessToken = "") { body = body ?? ""; SetCertificatePolicy(); ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; using (HttpClient client = new HttpClient()) { client.Timeout = new TimeSpan(0, 0, timeOut); if (headers != null) { foreach (var header in headers) { client.DefaultRequestHeaders.Add(header.Key, header.Value); } client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); } using (HttpContent httpContent = new StringContent(body, Encoding.UTF8)) { if (contentType != null) httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType); HttpResponseMessage response = client.PostAsync(url, httpContent).Result; return response.Content.ReadAsStringAsync().Result; } } } /// <summary> /// 发起GET同步请求 /// </summary> /// <param name="url">请求地址</param> /// <param name="headers">请求头</param> /// <param name="timeOut">超时时间</param> /// <returns></returns> public static string HttpGet(string url, Dictionary<string, string> headers = null, int timeOut = 30, string accessToken = "") { SetCertificatePolicy(); ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; using (HttpClient client = new HttpClient()) { client.Timeout = new TimeSpan(0, 0, timeOut); if (headers != null) { foreach (var header in headers) { client.DefaultRequestHeaders.Add(header.Key, header.Value); } client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); } HttpResponseMessage response = client.GetAsync(url).Result; return response.Content.ReadAsStringAsync().Result; } } /// <summary> /// 发起GET同步请求 /// </summary> /// <param name="url">请求地址</param> /// <param name="headers">请求头</param> /// <param name="timeOut">超时时间</param> /// <returns></returns> public static string HttpGet5(string url, Dictionary<string, string> headers = null, int timeOut = 30, string accessToken = "") { SetCertificatePolicy(); ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; using (HttpClient client = new HttpClient()) { client.Timeout = new TimeSpan(0, 0, timeOut); if (headers != null) { foreach (var header in headers) { client.DefaultRequestHeaders.Add(header.Key, header.Value); } client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); } HttpResponseMessage response = client.GetAsync(url).Result;//关键地方 return response.Content.ReadAsStringAsync().Result; } } public static HttpResponseMessage HttpGet6(string url, Dictionary<string, string> headers = null, int timeOut = 30, string accessToken = "") { SetCertificatePolicy(); ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; using (HttpClient client = new HttpClient()) { client.Timeout = new TimeSpan(0, 0, timeOut); if (headers != null) { foreach (var header in headers) { client.DefaultRequestHeaders.Add(header.Key, header.Value); } client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); } HttpResponseMessage response = client.GetAsync(url).Result;//关键地方 return response; } } public static string HttpDelete7(string url, Dictionary<string, string> headers = null, int timeOut = 30, string accessToken = "") { SetCertificatePolicy(); ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; using (HttpClient client = new HttpClient()) { client.Timeout = new TimeSpan(0, 0, timeOut); if (headers != null) { foreach (var header in headers) { client.DefaultRequestHeaders.Add(header.Key, header.Value); } } if (string.IsNullOrEmpty(accessToken)) { client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); } HttpResponseMessage response = client.DeleteAsync(url).Result;//关键地方 return response.Content.ReadAsStringAsync().Result; } } public static string SFHttpGet(string url, Dictionary<string, string> headers = null, int timeOut = 3000) { SetCertificatePolicy(); ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; using (HttpClient client = new HttpClient()) { client.Timeout = new TimeSpan(0, 0, timeOut); if (headers != null) { foreach (var header in headers) { client.DefaultRequestHeaders.Add(header.Key, header.Value); } } HttpResponseMessage response = client.GetAsync(url).Result; return response.Content.ReadAsStringAsync().Result; } } public static string HttpPATCH(string url, string body = null, string contentType = null, Dictionary<string, string> headers = null, int timeOut = 30) { body = body ?? ""; SetCertificatePolicy(); ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; using (HttpClient client = new HttpClient()) { client.Timeout = new TimeSpan(0, 0, timeOut); if (headers != null) { foreach (var header in headers) client.DefaultRequestHeaders.Add(header.Key, header.Value); } using (HttpContent httpContent = new StringContent(body, Encoding.UTF8)) { if (contentType != null) httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType); HttpRequestMessage request = new HttpRequestMessage(new HttpMethod("PATCH"), url); request.Content = httpContent; HttpResponseMessage response = client.SendAsync(request).Result; return response.Content.ReadAsStringAsync().Result; } } } // 对转码后的字符进行大写转换,不会把参数转换成大写 public static string UrlEncodeToJava(string source) { StringBuilder builder = new StringBuilder(); foreach (char c in source) { if (HttpUtility.UrlEncode(c.ToString(), Encoding.UTF8).Length > 1) { builder.Append(HttpUtility.UrlEncode(c.ToString(), Encoding.UTF8).ToUpper()); } else { builder.Append(c); } } string encodeUrl = builder.ToString().Replace("(", "%28").Replace(")", "%29"); return encodeUrl; } public static string SendHttpRequest33(string url, string Body = "", string contentType = "application/x-www-form-urlencoded", int Timeout = 3000) { byte[] sendData = Encoding.UTF8.GetBytes(Body);//加了一层编码 // byte[] sendData = Encoding.UTF8.GetBytes(Body);//加了一层编码 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.Timeout = Timeout; // 设置超时时间 //if (contentType != null) //{ // request.ContentType = contentType; //} using (Stream sendStream = request.GetRequestStream()) { sendStream.Write(sendData, 0, sendData.Length); sendStream.Close(); } using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { using (Stream stream = response.GetResponseStream()) { StreamReader reader = new StreamReader(stream, Encoding.UTF8); return reader.ReadToEnd(); } } } public static string HttpPostCaiNiao(string url, SortedDictionary<string, string> dictionary, string contentType = null, Dictionary<string, string> headers = null, int timeOut = 3000) { using (HttpClient client = new HttpClient()) { client.Timeout = new TimeSpan(0, 0, timeOut); if (headers != null) { foreach (var header in headers) { client.DefaultRequestHeaders.Add(header.Key, header.Value); } } using (HttpContent httpContent = new FormUrlEncodedContent(dictionary)) { if (contentType != null) httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType); HttpResponseMessage response = client.PostAsync(url, httpContent).Result; return response.Content.ReadAsStringAsync().Result; } } } public static string HttpPost10(string url, SortedDictionary<string, string> dictionary, string contentType = null, Dictionary<string, string> headers = null, int timeOut = 3000) { SetCertificatePolicy(); ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; using (HttpClient client = new HttpClient()) { client.Timeout = new TimeSpan(0, 0, timeOut); if (headers != null) { foreach (var header in headers) { client.DefaultRequestHeaders.Add(header.Key, header.Value); } } using (HttpContent httpContent = new FormUrlEncodedContent(dictionary)) { if (contentType != null) httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType); HttpResponseMessage response = client.PostAsync(url, httpContent).Result; return response.Content.ReadAsStringAsync().Result; } } } }
三 请求参数代码
数字人合成实体
public class CreatedModel { /// <summary> /// 1回调地址 /// </summary> public string callback_url { get; set; } /// <summary> /// 2 场景任务的ID /// </summary> public string scene_task_id { get; set; } /// <summary> /// 3 音频地址 /// </summary> public string audio_url { get; set; } } public class ResCreatedModel { public int code { get; set; } public string msg { get; set; } public VideoData data { get; set; } } public class VideoData { public int video_task_id { get; set; } }
数字人克隆实体
public class SceneModel { /// <summary> /// 1回调地址 /// </summary> public string callback_url { get; set; } /// <summary> /// 2 场景名称 /// </summary> public string video_name { get; set; } /// <summary> /// 3 场景的视频地址 /// </summary> public string video_url { get; set; } } public class ResSceneModel { public int code { get; set; } public string msg { get; set; } public SceneData data { get; set; } } public class SceneData { public int scene_task_id { get; set; } }
AI文案生成对话实体
public class ChatModel { /// <summary> /// 1 指令内容 /// </summary> public string prompt { get; set; } /// <summary> /// 2 需要执行的内容 /// </summary> public string content { get; set; } } public class ResChatModel { public int code { get; set; } public string msg { get; set; } public string data { get; set; } }
四 运行请求结果
其他两个接口没有体验,因为没有回调网站,所以不试了。感受确实如群友说的对接非常简单。几步就可以搞定。大家感兴趣可以去体验试试:api.yidevs.com/control/#/l…