开发者社区> 问答> 正文

语音服务的请求签名有哪些?

语音服务的请求签名有哪些?

展开
收起
保持可爱mmm 2020-03-27 00:39:43 430 0
1 条回答
写回答
取消 提交回答
  • tring()); public static String sign(String accessSecret, String stringToSign) throws Exception { javax.crypto.Mac mac = javax.crypto.Mac.getInstance("HmacSHA1"); mac.init(new javax.crypto.spec.SecretKeySpec(accessSecret.getBytes("UTF-8"), "HmacSHA1")); byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8")); return new sun.misc.BASE64Encoder().encode(signData); }

    参数说明:

    accessSecret:您的AccessKeyId对应的秘钥AccessSecret,特别说明:POP要求需要后面多加一个“&”字符,即accessSecret + “&”。
    stringToSign:即第三步生成的待签名请求串。
    

    签名后的结果打印如下:

    aMfgrx8DLS7vLfpeR1c2rrKLr0Q=

    步骤五:增加签名结果到请求参数中,发送请求 说明 签名也要做特殊URL编码。

    String Signature = specialUrlEncode(sign);// aMfgrx8DLS7vLfpeR1c2rrKLr0Q%3D

    最终完整的GET请求HTTP为:

    http://dyvmsapi.aliyuncs.com/?Signature=aMfgrx8DLS7vLfpeR1c2rrKLr0Q%3D&AccessKeyId=testId&Action=SingleCallByTts&CalledNumber=13000000000&CalledShowNumber=057112345678&Format=XML&OutId=123&RegionId=cn-hangzhou&SignatureMethod=HMAC-SHA1&SignatureNonce=f7d2d4ef-6d5f-4da4-86ed-88e001a66abb&SignatureVersion=1.0&Timestamp=2017-09-28T14%3A31%3A56Z&TtsCode=TTS_0000000&TtsParam=%7B%22code%22%3A%221234%22%2C%22product%22%3A%22test%22%7D&Version=2017-05-25

    JAVA示例 完整的Java签名Demo代码:

    public class VmsSignDemo { public static void main(String[] args) throws Exception { String accessKeyId = "testId"; String accessSecret = "testSecret"; java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); df.setTimeZone(new java.util.SimpleTimeZone(0, "GMT"));// 这里一定要设置GMT时区 java.util.Map<String, String> paras = new java.util.HashMap<String, String>(); // 1. 系统参数 paras.put("SignatureMethod", "HMAC-SHA1"); paras.put("SignatureNonce", java.util.UUID.randomUUID().toString()); paras.put("AccessKeyId", accessKeyId); paras.put("SignatureVersion", "1.0"); paras.put("Timestamp", df.format(new java.util.Date())); paras.put("Format", "XML"); // 2. 业务API参数 paras.put("Action", "SingleCallByTts"); paras.put("Version", "2017-05-25"); paras.put("RegionId", "cn-hangzhou"); paras.put("CalledShowNumber", "057112345678"); paras.put("CalledNumber", "13000000000"); paras.put("TtsParam", "{"code":"1234","product":"test"}"); paras.put("TtsCode", "TTS_0000000"); paras.put("OutId", "123"); // 3. 去除签名关键字Key if (paras.containsKey("Signature")) paras.remove("Signature"); // 4. 参数KEY排序 java.util.TreeMap<String, String> sortParas = new java.util.TreeMap<String, String>(); sortParas.putAll(paras); // 5. 构造待签名的字符串 java.util.Iterator it = sortParas.keySet().iterator(); StringBuilder sortQueryStringTmp = new StringBuilder(); while (it.hasNext()) { String key = it.next(); sortQueryStringTmp.append("&").append(specialUrlEncode(key)).append("=").append(specialUrlEncode(paras.get(key))); } String sortedQueryString = sortQueryStringTmp.substring(1);// 去除第一个多余的&符号 StringBuilder stringToSign = new StringBuilder(); stringToSign.append("GET").append("&"); stringToSign.append(specialUrlEncode("/")).append("&"); stringToSign.append(specialUrlEncode(sortedQueryString)); String sign = sign(accessSecret + "&", stringToSign.toString()); // 6. 签名最后也要做特殊URL编码 String signature = specialUrlEncode(sign); System.out.println(paras.get("SignatureNonce")); System.out.println("\r\n=========\r\n"); System.out.println(paras.get("Timestamp")); System.out.println("\r\n=========\r\n"); System.out.println(sortedQueryString); System.out.println("\r\n=========\r\n"); System.out.println(stringToSign.toString()); System.out.println("\r\n=========\r\n"); System.out.println(sign); System.out.println("\r\n=========\r\n"); System.out.println(signature); System.out.println("\r\n=========\r\n"); // 最终打印出合法GET请求的URL System.out.println("http://dyvmsapi.aliyuncs.com/?Signature=" + signature + sortQueryStringTmp); } public static String specialUrlEncode(String value) throws Exception { return java.net.URLEncoder.encode(value, "UTF-8").replace("+", "%20").replace("*", "%2A").replace("%7E", "~"); } public static String sign(String accessSecret, String stringToSign) throws Exception { javax.crypto.Mac mac = javax.crypto.Mac.getInstance("HmacSHA1"); mac.init(new javax.crypto.spec.SecretKeySpec(accessSecret.getBytes("UTF-8"), "HmacSHA1")); byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8")); return new sun.misc.BASE64Encoder().encode(signData); } } `

    2020-03-27 00:41:11
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载

相关实验场景

更多