public string GetSign(string methods, string url, string parameters, string headers) { /* * q-sign-algorithm=sha1 * &q-ak=[SecretID] * &q-sign-time=[SignTime] * &q-key-time=[KeyTime] * &q-header-list=[SignedHeaderList] * &q-url-param-list=[SignedParameterList] * &q-signature=[Signature] */ var now = UtilityTime.GetTimeStampByUtcNow(); var exp = cosKeyData.expiredTime; string singtime = now + ";" + exp; string signKey = Utility.Encryption.HmacSha1Sign(singtime, cosKeyData.tmpSecretKey); //Debug.Log("计算结果signKey " + signKey); string httpString = methods + "\n" + url + "\n" + parameters + "\n" + headers + "\n"; //Debug.Log("计算结果 " + httpString); string stringToSign = "sha1\n" + singtime + "\n" + Utility.Encryption.EncryptToSHA1(httpString) + "\n"; //Debug.Log("计算结果 " + stringToSign); string signature = Utility.Encryption.HmacSha1Sign(stringToSign, signKey); //Debug.Log("计算结果 " + signature); StringBuilder authorization = new StringBuilder(); authorization.Append("q-sign-algorithm=sha1"); authorization.Append("&q-ak="); authorization.Append(cosKeyData.tmpSecretId); authorization.Append("&q-sign-time="); authorization.Append(singtime); authorization.Append("&q-key-time="); authorization.Append(singtime); authorization.Append("&q-header-list="); authorization.Append(parameters); authorization.Append("&q-url-param-list="); authorization.Append(headers); authorization.Append("&q-signature="); authorization.Append(signature); string authStr = authorization.ToString(); Debug.Log("sign " + authStr); return(authStr); }
/// <summary> /// 发送语音识别 /// </summary> /// <param name="audio">AudioClip 音频</param> /// <param name="complete">回调结果</param> /// <param name="engineType"> 普通话(sms16k),普通话(sms8k),英语(sms-en8k),英语(sms-en16k) </param> public void PostIAT(byte[] audio, UnityAction <string> complete, EngineType engineType = EngineType.SmsEn16k) { if (audio == null || audio.Length == 0) { Debug.LogErrorFormat("[XFYunIAT].PostIAT audio error null or count=0. "); if (complete != null) { complete.Invoke(string.Empty); } return; } string curTime = UtilityTime.GetTimeStampStrByUtcNow(); string param = "{\"aue\":\"" + Aue + "\"" + ",\"engine_type\":\"" + GetEngineType(engineType) + "\"}"; byte[] bytedata = Encoding.ASCII.GetBytes(param); string x_param = Convert.ToBase64String(bytedata); string result = string.Format("{0}{1}{2}", AppKey, curTime, x_param); string X_checksum = Md5(result); HTTPRequest request = new HTTPRequest(new Uri(Host), HTTPMethods.Post, (req, resp) => { if (resp.StatusCode == 200) { Debug.LogFormat("[XFYunIAT].PostIAT complete ."); Debug.LogFormat("返回结果 '{0}' .", resp.DataAsText); if (complete != null) { complete.Invoke(resp.DataAsText); } } else { Debug.LogErrorFormat("[XFYunIAT].PostIAT code error code : '{0}' , data :'{1}' , msg : '{2}'. ", resp.StatusCode, resp.DataAsText, resp.Message); if (complete != null) { complete.Invoke(string.Empty); } } }); request.AddHeader("Content-Type", "application/x-www-form-urlencoded"); request.AddHeader("charset", "utf-8"); request.AddHeader("X-Appid", AppId); request.AddHeader("X-CurTime", curTime); request.AddHeader("X-Param", x_param); request.AddHeader("X-CheckSum", X_checksum); string audioStr = Convert.ToBase64String(audio); request.AddField("audio", audioStr); request.Send(); }
/// <summary> /// 压缩zip /// </summary> /// <param name="tempWorkPath">临时工作路径</param> /// <param name="fileName"></param> /// <param name="fileList"></param> /// <returns></returns> public static byte[] EnZipBytes(string tempWorkPath, string[] fileName, List <byte[]> fileList) { if (fileName == null || fileList == null || (fileName.Length != fileList.Count)) { Debug.LogError("not s3 pose files."); return(null); } string fname = UtilityTime.GetTimeStampStrByUtcNow() + ".zip"; string zipFileName = Utility.Path.GetCombinePath(tempWorkPath, fname); // 获取工作目录 EnZipFile(zipFileName, fileName, fileList); // 把字节流打包成zip byte[] bytes = File.ReadAllBytes(zipFileName); // 把文件读取到内存中 File.Delete(zipFileName); // 删除本地硬盘上文件 return(bytes); }
private CosKeyDataModel cosKeyData; // cosKeyData = WebRequestResult.JsonToObject<CosKeyDataModel>("cos_key_dict"); #region ... API /// <summary> /// 检查 cos key有效期 /// </summary> /// <param name="complete"></param> public void CheckCosKey(UnityAction complete = null) { if (cosKeyData != null && !cosKeyData.IsNull) { long tmpCosKeyTime = UtilityTime.GetTimeStampByUtcNow(); // cosKeyData.expiredTime; long nowCosKeyTime = UtilityTime.GetTimeStampByUtcNow(); if ((tmpCosKeyTime - nowCosKeyTime) > COS_KEY_TIMEOUT) // 密钥在有效时间内 { if (complete != null) { complete(); } return; } } // TODO:发送获取 cos key 请求 key从服务器获取 }