ToEqbTest.java 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247
  1. package com.malk.hengyilong.mjavahengyilong;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.malk.hengyilong.utils.HTTPHelper;
  4. import com.malk.server.common.McR;
  5. import lombok.extern.slf4j.Slf4j;
  6. import org.apache.commons.codec.binary.Base64;
  7. import org.junit.jupiter.api.Test;
  8. import javax.crypto.Mac;
  9. import javax.crypto.spec.SecretKeySpec;
  10. import java.io.UnsupportedEncodingException;
  11. import java.net.URLEncoder;
  12. import java.security.InvalidKeyException;
  13. import java.security.MessageDigest;
  14. import java.security.NoSuchAlgorithmException;
  15. import java.text.MessageFormat;
  16. import java.util.*;
  17. /**
  18. * 功能:
  19. * 作者:hanxue
  20. * 日期:2026/4/20 16:29
  21. */
  22. @Slf4j
  23. public class ToEqbTest {
  24. @Test
  25. public void main() {
  26. // // 应用ID
  27. // String appId = "7439110455";
  28. // // 应用密钥(AppSecret)
  29. // String appKey = "ae304291c5702141ea7c98bcdaae6a17";
  30. // // e签宝接口调用域名(模拟环境)
  31. // String host = "https://smlopenapi.esign.cn";
  32. //// fileId
  33. // String fileId ="933318bf1d204d1e8b9816a77ef9e19d";
  34. // // docTemplateId
  35. // String docTemplateId ="ee7bdf1404154c0686ed38a7cf65a7ff";
  36. // String filecontentMd5 ="nJsZC4SzKUudj0awHel4oQ==";
  37. // 应用ID
  38. String appId = "5112061694";
  39. // 应用密钥(AppSecret)
  40. String appKey = "7e464621b3aa4a0e39a6557009b38214";
  41. // e签宝接口调用域名(正式环境)
  42. String host = "https://openapi.esign.cn";
  43. // 报价单模板文件 fileId
  44. String fileId ="cacddedc33ff45cea8acd4e8a8f81ac4";
  45. // 报价单模板文件 docTemplateId
  46. String docTemplateId ="1d2f50e82f514144b72313b1dddcb552";//恒益隆模板id
  47. // String docTemplateId ="e7440c3f38724b239d7e9dcf7329804a";//绍贺模板id
  48. // 报价单模板文件 filecontentMd5
  49. String filecontentMd5 ="nJsZC4SzKUudj0awHel4oQ==";
  50. // 购销合同模板文件 fileId
  51. // String fileId ="c39361f5ca74466a81287f90e94f06c3";
  52. // // 购销合同模板文件docTemplateId
  53. //// 0064564bea704fc587a249680064b98c
  54. //// 0cc8c51361444dc7bd39385898893812
  55. //// 0064564bea704fc587a249680064b98c
  56. // String docTemplateId ="d481b2222879447f84d992bac20784d0";
  57. // // 购销合同模板文件 filecontentMd5
  58. // String filecontentMd5 ="kPALmlC0o3nU8O6U1HuaxQ==";
  59. //步骤1:上传本地文件并转成HTML格式
  60. //1.1获取文件上传地址
  61. // getFileUploadUrl(appId,appKey,host,filecontentMd5);
  62. //1.2上传文件流 postman
  63. //
  64. //步骤2:查看文件上传状态及详情
  65. // getFileInfo(appId,appKey,host,fileId);
  66. //
  67. //步骤3:获取制作合同模板页面链接
  68. // getDocCreateUrl(appId,appKey,host,fileId);
  69. //
  70. //
  71. // //访问【获取制作合同模板页面】接口返回的创建文件模板页面链接(docTemplateCreateUrl参数值),并在页面中拖动【动态表格】控件来制作模板,若链接失效,调用【获取编辑合同模板页面】接口
  72. // getDocTemplateEditUrl(appId,appKey,host,docTemplateId);
  73. //
  74. //
  75. // 复制合同模板
  76. // copyDocTemplate(appId,appKey,host,docTemplateId,"报价单-绍贺");
  77. // //获取 HTML 模板中控件ID和控件Key
  78. // getDocTemplateInfo(appId,appKey,host,docTemplateId);
  79. //
  80. //步骤6:构造数据并填充模板生成文件
  81. //6.1填写模板生成文件
  82. // createByDocTemplate(appId,appKey,host,docTemplateId);
  83. // //6.2开发者使用【查询文件上传状态】接口获取填充后文件链接,通过此链接可查看填充后文件效果
  84. // 253a487cfc1c464190006e7b8532479f
  85. //b97b73a033a341129bf788281a064f43
  86. getFileInfo(appId,appKey,host,"cc1106f83a1a40b99e450eb10863bb9d");
  87. //基于文件发起签署
  88. // createByFile(appId,appKey,host,"c62b5694b6bc447299ef088146d39111");
  89. // //查询签署流程详情
  90. // getSignFlowDetail(appId,appKey,host,"62dc01292d684653bae341adb014b4d0");
  91. //
  92. // //下载签署文件
  93. // downloadFile(appId,appKey,host,"8ef134f1e4e648acba2aa0cbe822bd55");
  94. //查询机构认证信息
  95. // getOrgInfo(appId,appKey,host,"上海绍贺贸易有限公司 ");
  96. // getOrgUserInfo(appId,appKey,host,"fed0042d62a848de9052de45b31b83ea");
  97. // getOrgMember(appId,appKey,host,"04dd6dbad3d8456b9e551fc329c516d7");
  98. // 查询个人认证信息
  99. // getPersonInfo(appId,appKey,host,"13482241481");
  100. //获取签署地址
  101. // getSignUrl(appId,appKey,host,"10fa0c28225a45a4ab31d8a7b98cb601","0fd3eb8b0c424b4e827bb3bf1fba62f3");
  102. // 请求签名鉴权-POST请求
  103. // testPost(appId, appKey, host);
  104. // 请求签名鉴权-GET请求
  105. // String signFlowId = "e622498****ebf72d57dbb";
  106. // testGet(appId, appKey, host, signFlowId);
  107. }
  108. /***
  109. * 获取文件上传地址
  110. */
  111. public static void getFileUploadUrl(String appId, String appKey, String host,String filecontentMd5) {
  112. // 计算签名拼接的url
  113. String postUrl = "/v3/files/file-upload-url";
  114. // 完整的请求地址
  115. String postAllUrl = host + postUrl;
  116. try {
  117. // 构建请求Body体
  118. JSONObject reqBodyObj = new JSONObject();
  119. reqBodyObj.put("contentMd5", filecontentMd5);
  120. reqBodyObj.put("contentType", "application/octet-stream");
  121. reqBodyObj.put("convertToHTML", true);
  122. // reqBodyObj.put("fileName", "hengyilong.docx");
  123. // reqBodyObj.put("fileSize", "24456 ");
  124. reqBodyObj.put("fileName", "购销合同模板.docx");
  125. reqBodyObj.put("fileSize", "33095 ");
  126. // 请求Body体数据
  127. String reqBodyData = reqBodyObj.toString();
  128. // 对请求Body体内的数据计算ContentMD5
  129. String contentMD5 = doContentMD5(reqBodyData);
  130. System.out.println("请求body数据:"+reqBodyData);
  131. System.out.println("body的md5值:"+ contentMD5);
  132. // 构建待签名字符串
  133. String method = "POST";
  134. String accept = "*/*";
  135. String contentType = "application/json";
  136. String url = postUrl;
  137. String date = "";
  138. String headers = "";
  139. StringBuffer sb = new StringBuffer();
  140. sb.append(method).append("\n").append(accept).append("\n").append(contentMD5).append("\n")
  141. .append(contentType).append("\n").append(date).append("\n");
  142. if ("".equals(headers)) {
  143. sb.append(headers).append(url);
  144. } else {
  145. sb.append(headers).append("\n").append(url);
  146. }
  147. // 构建参与请求签名计算的明文
  148. String plaintext = sb.toString();
  149. // 计算请求签名值
  150. String reqSignature = doSignatureBase64(plaintext, appKey);
  151. System.out.println("计算请求签名值:"+reqSignature);
  152. // 获取时间戳(精确到毫秒)
  153. long timeStamp = timeStamp();
  154. // 构建请求头
  155. LinkedHashMap<String, String> header = new LinkedHashMap<String, String>();
  156. header.put("X-Tsign-Open-App-Id", appId);
  157. header.put("X-Tsign-Open-Auth-Mode", "Signature");
  158. header.put("X-Tsign-Open-Ca-Timestamp", String.valueOf(timeStamp));
  159. header.put("Accept", accept);
  160. header.put("Content-Type", contentType);
  161. header.put("X-Tsign-Open-Ca-Signature", reqSignature);
  162. header.put("Content-MD5", contentMD5);
  163. // 发送POST请求
  164. String result = HTTPHelper.sendPOST(postAllUrl, reqBodyData, header, "UTF-8");
  165. JSONObject resultObj = JSONObject.parseObject(result);
  166. System.out.println("请求返回信息: " + resultObj.toString());
  167. } catch (Exception e) {
  168. e.printStackTrace();
  169. String msg = MessageFormat.format("请求签名鉴权方式调用接口出现异常: {0}", e.getMessage());
  170. System.out.println(msg);
  171. }
  172. }
  173. /***
  174. * 计算请求Body体的Content-MD5
  175. * @param str 待计算的消息
  176. * @return MD5计算后摘要值的Base64编码(ContentMD5)
  177. * @throws Exception 加密过程中的异常信息
  178. */
  179. public static String doContentMD5(String str) throws Exception {
  180. byte[] md5Bytes = null;
  181. MessageDigest md5 = null;
  182. String contentMD5 = null;
  183. try {
  184. md5 = MessageDigest.getInstance("MD5");
  185. // 计算md5函数
  186. md5.update(str.getBytes("UTF-8"));
  187. // 获取文件MD5的二进制数组(128位)
  188. md5Bytes = md5.digest();
  189. // 对Body体MD5的二进制数组(128位)进行Base64编码(而不是对32位的16进制字符串进行编码)
  190. contentMD5 = new String(Base64.encodeBase64(md5Bytes), "UTF-8");
  191. } catch (NoSuchAlgorithmException e) {
  192. String msg = MessageFormat.format("不支持此算法: {0}", e.getMessage());
  193. Exception ex = new Exception(msg);
  194. ex.initCause(e);
  195. throw ex;
  196. } catch (UnsupportedEncodingException e) {
  197. String msg = MessageFormat.format("不支持的字符编码: {0}", e.getMessage());
  198. Exception ex = new Exception(msg);
  199. ex.initCause(e);
  200. throw ex;
  201. }
  202. return contentMD5;
  203. }
  204. public static long timeStamp() {
  205. long timeStamp = System.currentTimeMillis();
  206. return timeStamp;
  207. }
  208. /***
  209. * 计算请求签名值
  210. *
  211. * @param message 待计算的消息
  212. * @param secret 密钥
  213. * @return HmacSHA256计算后摘要值的Base64编码
  214. * @throws Exception 加密过程中的异常信息
  215. */
  216. public static String doSignatureBase64(String message, String secret) throws Exception {
  217. String algorithm = "HmacSHA256";
  218. Mac hmacSha256;
  219. String digestBase64 = null;
  220. try {
  221. hmacSha256 = Mac.getInstance(algorithm);
  222. byte[] keyBytes = secret.getBytes("UTF-8");
  223. byte[] messageBytes = message.getBytes("UTF-8");
  224. hmacSha256.init(new SecretKeySpec(keyBytes, 0, keyBytes.length, algorithm));
  225. // 使用HmacSHA256对二进制数据消息Bytes计算摘要
  226. byte[] digestBytes = hmacSha256.doFinal(messageBytes);
  227. // 把摘要后的结果digestBytes使用Base64进行编码
  228. digestBase64 = new String(Base64.encodeBase64(digestBytes), "UTF-8");
  229. } catch (NoSuchAlgorithmException e) {
  230. String msg = MessageFormat.format("不支持此算法: {0}", e.getMessage());
  231. Exception ex = new Exception(msg);
  232. ex.initCause(e);
  233. throw ex;
  234. } catch (UnsupportedEncodingException e) {
  235. String msg = MessageFormat.format("不支持的字符编码: {0}", e.getMessage());
  236. Exception ex = new Exception(msg);
  237. ex.initCause(e);
  238. throw ex;
  239. } catch (InvalidKeyException e) {
  240. String msg = MessageFormat.format("无效的密钥规范: {0}", e.getMessage());
  241. Exception ex = new Exception(msg);
  242. ex.initCause(e);
  243. throw ex;
  244. }
  245. return digestBase64;
  246. }
  247. /**
  248. * 查询文件上传状态
  249. */
  250. public static void getFileInfo(String appId, String appKey, String host,String fileId) {
  251. // 计算签名拼接的url
  252. String getUrl = "/v3/files/"+fileId;
  253. // 完整的请求地址
  254. String getAllUrl = host + getUrl;
  255. try {
  256. // GET请求时ContentMD5为""
  257. String contentMD5 = "";
  258. // 构建待签名字符串
  259. String method = "GET";
  260. String accept = "*/*";
  261. String contentType = "application/json; charset=UTF-8";
  262. String url = getUrl;
  263. String date = "";
  264. String headers = "";
  265. StringBuffer sb = new StringBuffer();
  266. sb.append(method).append("\n").append(accept).append("\n").append(contentMD5).append("\n")
  267. .append(contentType).append("\n").append(date).append("\n");
  268. if ("".equals(headers)) {
  269. sb.append(headers).append(url);
  270. } else {
  271. sb.append(headers).append("\n").append(url);
  272. }
  273. // 构建参与请求签名计算的明文
  274. String plaintext = sb.toString();
  275. // 计算请求签名值
  276. String reqSignature = doSignatureBase64(plaintext, appKey);
  277. System.out.println("计算请求签名值:"+ reqSignature);
  278. // 获取时间戳(精确到毫秒)
  279. long timeStamp = timeStamp();
  280. // 构建请求头
  281. LinkedHashMap<String, String> header = new LinkedHashMap<String, String>();
  282. header.put("X-Tsign-Open-App-Id", appId);
  283. header.put("X-Tsign-Open-Auth-Mode", "Signature");
  284. header.put("X-Tsign-Open-Ca-Signature", reqSignature);
  285. header.put("X-Tsign-Open-Ca-Timestamp", String.valueOf(timeStamp));
  286. header.put("Accept", accept);
  287. header.put("Content-Type", contentType);
  288. header.put("Content-MD5", contentMD5);
  289. HashMap<String, Object> query = new HashMap<String, Object>();
  290. // query.put("orgIDCardNum", "9100*****0004");
  291. // query.put("orgIDCardType", "CRED_ORG_USCC");
  292. // 发送GET请求
  293. String result = HTTPHelper.sendGet(getAllUrl, query, header, "UTF-8");
  294. JSONObject resultObj = JSONObject.parseObject(result);
  295. System.out.println("请求返回信息: " + resultObj.toString());
  296. } catch (Exception e) {
  297. e.printStackTrace();
  298. String msg = MessageFormat.format("请求签名鉴权方式调用接口出现异常: {0}", e.getMessage());
  299. System.out.println(msg);
  300. }
  301. }
  302. /***
  303. * 获取制作合同模板页面
  304. */
  305. public static void getDocCreateUrl(String appId, String appKey, String host,String fileId) {
  306. // 计算签名拼接的url
  307. String postUrl = "/v3/doc-templates/doc-template-create-url";
  308. // 完整的请求地址
  309. String postAllUrl = host + postUrl;
  310. try {
  311. // 构建请求Body体
  312. JSONObject reqBodyObj = new JSONObject();
  313. reqBodyObj.put("docTemplateName", "购销合同");
  314. reqBodyObj.put("docTemplateType", 1);
  315. reqBodyObj.put("fileId", fileId);
  316. // reqBodyObj.put("hiddenOriginComponents", "false");
  317. // 请求Body体数据
  318. String reqBodyData = reqBodyObj.toString();
  319. // 对请求Body体内的数据计算ContentMD5
  320. String contentMD5 = doContentMD5(reqBodyData);
  321. System.out.println("请求body数据:"+reqBodyData);
  322. System.out.println("body的md5值:"+ contentMD5);
  323. // 构建待签名字符串
  324. String method = "POST";
  325. String accept = "*/*";
  326. String contentType = "application/json";
  327. String url = postUrl;
  328. String date = "";
  329. String headers = "";
  330. StringBuffer sb = new StringBuffer();
  331. sb.append(method).append("\n").append(accept).append("\n").append(contentMD5).append("\n")
  332. .append(contentType).append("\n").append(date).append("\n");
  333. if ("".equals(headers)) {
  334. sb.append(headers).append(url);
  335. } else {
  336. sb.append(headers).append("\n").append(url);
  337. }
  338. // 构建参与请求签名计算的明文
  339. String plaintext = sb.toString();
  340. // 计算请求签名值
  341. String reqSignature = doSignatureBase64(plaintext, appKey);
  342. System.out.println("计算请求签名值:"+reqSignature);
  343. // 获取时间戳(精确到毫秒)
  344. long timeStamp = timeStamp();
  345. // 构建请求头
  346. LinkedHashMap<String, String> header = new LinkedHashMap<String, String>();
  347. header.put("X-Tsign-Open-App-Id", appId);
  348. header.put("X-Tsign-Open-Auth-Mode", "Signature");
  349. header.put("X-Tsign-Open-Ca-Timestamp", String.valueOf(timeStamp));
  350. header.put("Accept", accept);
  351. header.put("Content-Type", contentType);
  352. header.put("X-Tsign-Open-Ca-Signature", reqSignature);
  353. header.put("Content-MD5", contentMD5);
  354. // 发送POST请求
  355. String result = HTTPHelper.sendPOST(postAllUrl, reqBodyData, header, "UTF-8");
  356. JSONObject resultObj = JSONObject.parseObject(result);
  357. System.out.println("请求返回信息: " + resultObj.toString());
  358. } catch (Exception e) {
  359. e.printStackTrace();
  360. String msg = MessageFormat.format("请求签名鉴权方式调用接口出现异常: {0}", e.getMessage());
  361. System.out.println(msg);
  362. }
  363. }
  364. /***
  365. * 获取编辑合同模板页面
  366. */
  367. public static void getDocTemplateEditUrl(String appId, String appKey, String host,String docTemplateId) {
  368. // 计算签名拼接的url
  369. String postUrl = "/v3/doc-templates/" + docTemplateId + "/doc-template-edit-url";
  370. // 完整的请求地址
  371. String postAllUrl = host + postUrl;
  372. try {
  373. // 构建请求Body体
  374. JSONObject reqBodyObj = new JSONObject();
  375. // 请求Body体数据
  376. String reqBodyData = reqBodyObj.toString();
  377. // 对请求Body体内的数据计算ContentMD5
  378. String contentMD5 = doContentMD5(reqBodyData);
  379. System.out.println("请求body数据:"+reqBodyData);
  380. System.out.println("body的md5值:"+ contentMD5);
  381. // 构建待签名字符串
  382. String method = "POST";
  383. String accept = "*/*";
  384. String contentType = "application/json";
  385. String url = postUrl;
  386. String date = "";
  387. String headers = "";
  388. StringBuffer sb = new StringBuffer();
  389. sb.append(method).append("\n").append(accept).append("\n").append(contentMD5).append("\n")
  390. .append(contentType).append("\n").append(date).append("\n");
  391. if ("".equals(headers)) {
  392. sb.append(headers).append(url);
  393. } else {
  394. sb.append(headers).append("\n").append(url);
  395. }
  396. // 构建参与请求签名计算的明文
  397. String plaintext = sb.toString();
  398. // 计算请求签名值
  399. String reqSignature = doSignatureBase64(plaintext, appKey);
  400. System.out.println("计算请求签名值:"+reqSignature);
  401. // 获取时间戳(精确到毫秒)
  402. long timeStamp = timeStamp();
  403. // 构建请求头
  404. LinkedHashMap<String, String> header = new LinkedHashMap<String, String>();
  405. header.put("X-Tsign-Open-App-Id", appId);
  406. header.put("X-Tsign-Open-Auth-Mode", "Signature");
  407. header.put("X-Tsign-Open-Ca-Timestamp", String.valueOf(timeStamp));
  408. header.put("Accept", accept);
  409. header.put("Content-Type", contentType);
  410. header.put("X-Tsign-Open-Ca-Signature", reqSignature);
  411. header.put("Content-MD5", contentMD5);
  412. // 发送POST请求
  413. String result = HTTPHelper.sendPOST(postAllUrl, reqBodyData, header, "UTF-8");
  414. JSONObject resultObj = JSONObject.parseObject(result);
  415. System.out.println("请求返回信息: " + resultObj.toString());
  416. } catch (Exception e) {
  417. e.printStackTrace();
  418. String msg = MessageFormat.format("请求签名鉴权方式调用接口出现异常: {0}", e.getMessage());
  419. System.out.println(msg);
  420. }
  421. }
  422. /***
  423. * 复制合同模板页面
  424. */
  425. public static void copyDocTemplate(String appId, String appKey, String host,String docTemplateId,String renameDocTemplate) {
  426. // 计算签名拼接的url
  427. String postUrl = "/v3/doc-templates/" + docTemplateId + "/copy";
  428. // 完整的请求地址
  429. String postAllUrl = host + postUrl;
  430. try {
  431. // 构建请求Body体
  432. JSONObject reqBodyObj = new JSONObject();
  433. reqBodyObj.put("renameDocTemplate", renameDocTemplate);
  434. // 请求Body体数据
  435. String reqBodyData = reqBodyObj.toString();
  436. // 对请求Body体内的数据计算ContentMD5
  437. String contentMD5 = doContentMD5(reqBodyData);
  438. System.out.println("请求body数据:"+reqBodyData);
  439. System.out.println("body的md5值:"+ contentMD5);
  440. // 构建待签名字符串
  441. String method = "POST";
  442. String accept = "*/*";
  443. String contentType = "application/json";
  444. String url = postUrl;
  445. String date = "";
  446. String headers = "";
  447. StringBuffer sb = new StringBuffer();
  448. sb.append(method).append("\n").append(accept).append("\n").append(contentMD5).append("\n")
  449. .append(contentType).append("\n").append(date).append("\n");
  450. if ("".equals(headers)) {
  451. sb.append(headers).append(url);
  452. } else {
  453. sb.append(headers).append("\n").append(url);
  454. }
  455. // 构建参与请求签名计算的明文
  456. String plaintext = sb.toString();
  457. // 计算请求签名值
  458. String reqSignature = doSignatureBase64(plaintext, appKey);
  459. System.out.println("计算请求签名值:"+reqSignature);
  460. // 获取时间戳(精确到毫秒)
  461. long timeStamp = timeStamp();
  462. // 构建请求头
  463. LinkedHashMap<String, String> header = new LinkedHashMap<String, String>();
  464. header.put("X-Tsign-Open-App-Id", appId);
  465. header.put("X-Tsign-Open-Auth-Mode", "Signature");
  466. header.put("X-Tsign-Open-Ca-Timestamp", String.valueOf(timeStamp));
  467. header.put("Accept", accept);
  468. header.put("Content-Type", contentType);
  469. header.put("X-Tsign-Open-Ca-Signature", reqSignature);
  470. header.put("Content-MD5", contentMD5);
  471. // 发送POST请求
  472. String result = HTTPHelper.sendPOST(postAllUrl, reqBodyData, header, "UTF-8");
  473. JSONObject resultObj = JSONObject.parseObject(result);
  474. System.out.println("请求返回信息: " + resultObj.toString());
  475. } catch (Exception e) {
  476. e.printStackTrace();
  477. String msg = MessageFormat.format("请求签名鉴权方式调用接口出现异常: {0}", e.getMessage());
  478. System.out.println(msg);
  479. }
  480. }
  481. /**
  482. * 查询合同模板中控件详情
  483. */
  484. public static void getDocTemplateInfo(String appId, String appKey, String host,String docTemplateId) {
  485. // 计算签名拼接的url
  486. String getUrl = "/v3/doc-templates/"+docTemplateId;
  487. // 完整的请求地址
  488. String getAllUrl = host + getUrl;
  489. try {
  490. // GET请求时ContentMD5为""
  491. String contentMD5 = "";
  492. // 构建待签名字符串
  493. String method = "GET";
  494. String accept = "*/*";
  495. String contentType = "application/json; charset=UTF-8";
  496. String url = getUrl;
  497. String date = "";
  498. String headers = "";
  499. StringBuffer sb = new StringBuffer();
  500. sb.append(method).append("\n").append(accept).append("\n").append(contentMD5).append("\n")
  501. .append(contentType).append("\n").append(date).append("\n");
  502. if ("".equals(headers)) {
  503. sb.append(headers).append(url);
  504. } else {
  505. sb.append(headers).append("\n").append(url);
  506. }
  507. // 构建参与请求签名计算的明文
  508. String plaintext = sb.toString();
  509. // 计算请求签名值
  510. String reqSignature = doSignatureBase64(plaintext, appKey);
  511. System.out.println("计算请求签名值:"+ reqSignature);
  512. // 获取时间戳(精确到毫秒)
  513. long timeStamp = timeStamp();
  514. // 构建请求头
  515. LinkedHashMap<String, String> header = new LinkedHashMap<String, String>();
  516. header.put("X-Tsign-Open-App-Id", appId);
  517. header.put("X-Tsign-Open-Auth-Mode", "Signature");
  518. header.put("X-Tsign-Open-Ca-Signature", reqSignature);
  519. header.put("X-Tsign-Open-Ca-Timestamp", String.valueOf(timeStamp));
  520. header.put("Accept", accept);
  521. header.put("Content-Type", contentType);
  522. header.put("Content-MD5", contentMD5);
  523. HashMap<String, Object> query = new HashMap<String, Object>();
  524. // query.put("orgIDCardNum", "9100*****0004");
  525. // query.put("orgIDCardType", "CRED_ORG_USCC");
  526. // 发送GET请求
  527. String result = HTTPHelper.sendGet(getAllUrl, query, header, "UTF-8");
  528. JSONObject resultObj = JSONObject.parseObject(result);
  529. System.out.println("请求返回信息: " + resultObj.toString());
  530. } catch (Exception e) {
  531. e.printStackTrace();
  532. String msg = MessageFormat.format("请求签名鉴权方式调用接口出现异常: {0}", e.getMessage());
  533. System.out.println(msg);
  534. }
  535. }
  536. /***
  537. * 填写模板生成文件
  538. */
  539. public static void createByDocTemplate(String appId, String appKey, String host,String docTemplateId) {
  540. // 计算签名拼接的url
  541. String postUrl = "/v3/files/create-by-doc-template";
  542. // 完整的请求地址
  543. String postAllUrl = host + postUrl;
  544. try {
  545. List<Map> components = new ArrayList<>();
  546. //我放公司
  547. Map company = new HashMap();
  548. company.put("componentKey", "company");
  549. company.put("componentValue", "上海恒益隆");
  550. Map companyEng = new HashMap();
  551. companyEng.put("componentKey", "companyEng");
  552. companyEng.put("componentValue", "Shanghai");
  553. Map companySeal = new HashMap();
  554. companySeal.put("componentKey", "companySeal");
  555. companySeal.put("componentValue", "上海恒益隆");
  556. //客户名称
  557. Map clientName = new HashMap();
  558. clientName.put("componentKey", "clientName");
  559. clientName.put("componentValue", "test1");
  560. //销售人员
  561. Map salesPerson = new HashMap();
  562. salesPerson.put("componentKey", "salesPerson");
  563. salesPerson.put("componentValue", "小韩");
  564. //联系人
  565. Map contactPerson = new HashMap();
  566. contactPerson.put("componentKey", "contactPerson");
  567. contactPerson.put("componentValue", "小韩");
  568. //供方联系方式
  569. Map contactInfo = new HashMap();
  570. contactInfo.put("componentKey", "contactInfo");
  571. contactInfo.put("componentValue", "15900010001");
  572. //需方地址
  573. Map address = new HashMap();
  574. address.put("componentKey", "address");
  575. address.put("componentValue", "上海市浦东新区");
  576. //供方电子邮箱
  577. Map email = new HashMap();
  578. email.put("componentKey", "email");
  579. email.put("componentValue", "xxx@163.com");
  580. //客户联系方式
  581. Map clientContactInfo = new HashMap();
  582. clientContactInfo.put("componentKey", "clientContactInfo");
  583. clientContactInfo.put("componentValue", "15252133552");
  584. // 报价日期
  585. Map quotationDate = new HashMap();
  586. quotationDate.put("componentKey", "quotationDate");
  587. quotationDate.put("componentValue", "2026-04-02");
  588. //客户邮箱
  589. Map clientEmail = new HashMap();
  590. clientEmail.put("componentKey", "clientEmail");
  591. clientEmail.put("componentValue", "xxx@qq.com");
  592. //有效期
  593. Map validityPeriod = new HashMap();
  594. validityPeriod.put("componentKey", "validityPeriod");
  595. validityPeriod.put("componentValue", "2026-11-02");
  596. //产品明细
  597. List<Map> productDetail = new ArrayList<>();
  598. productDetail.add(getRowMap(false));
  599. productDetail.add(getRowMap(false,"产品1","型号1","单位1","1.12","1.10","1"));
  600. productDetail.add(getRowMap(true,"产品2","型号2","单位2","2.12","2.10","2"));
  601. productDetail.add(getRowMap(true,"产品3","型号3","单位3","3.12","4.10","3"));
  602. productDetail.add(getRowMap(true,"产品4","型号4","单位4","4.12","4.10","4"));
  603. productDetail.add(getRowMap(true,"产品5","型号5","单位5","5.12","5.10","5"));
  604. productDetail.add(getRowMap(true,"产品6","型号6","单位6","6.12","6.10","6"));
  605. productDetail.add(getRowMap(true,"产品7","型号7","单位7","7.12","7.10","7"));
  606. productDetail.add(getRowMap(true,"产品8","型号8","单位8","8.12","8.10","8"));
  607. Map productDetailMap = new HashMap();
  608. productDetailMap.put("componentKey", "productDetail");
  609. productDetailMap.put("componentValue", JSONObject.toJSONString(productDetail));
  610. components.add(company);
  611. components.add(companyEng);
  612. components.add(companySeal);
  613. components.add(clientName);
  614. components.add(salesPerson);
  615. components.add(contactPerson);
  616. components.add(contactInfo);
  617. components.add(address);
  618. components.add(email);
  619. components.add(clientContactInfo);
  620. components.add(quotationDate);
  621. components.add(clientEmail);
  622. components.add(validityPeriod);
  623. components.add(productDetailMap);
  624. // 构建请求Body体
  625. JSONObject reqBodyObj = new JSONObject();
  626. reqBodyObj.put("docTemplateId", docTemplateId);
  627. reqBodyObj.put("fileName", "测试报价单-恒益隆.pdf");
  628. reqBodyObj.put("components", components);
  629. // 请求Body体数据
  630. String reqBodyData = reqBodyObj.toString();
  631. // 对请求Body体内的数据计算ContentMD5
  632. String contentMD5 = doContentMD5(reqBodyData);
  633. System.out.println("请求body数据:"+reqBodyData);
  634. System.out.println("body的md5值:"+ contentMD5);
  635. // 构建待签名字符串
  636. String method = "POST";
  637. String accept = "*/*";
  638. String contentType = "application/json";
  639. String url = postUrl;
  640. String date = "";
  641. String headers = "";
  642. StringBuffer sb = new StringBuffer();
  643. sb.append(method).append("\n").append(accept).append("\n").append(contentMD5).append("\n")
  644. .append(contentType).append("\n").append(date).append("\n");
  645. if ("".equals(headers)) {
  646. sb.append(headers).append(url);
  647. } else {
  648. sb.append(headers).append("\n").append(url);
  649. }
  650. // 构建参与请求签名计算的明文
  651. String plaintext = sb.toString();
  652. // 计算请求签名值
  653. String reqSignature = doSignatureBase64(plaintext, appKey);
  654. System.out.println("计算请求签名值:"+reqSignature);
  655. // 获取时间戳(精确到毫秒)
  656. long timeStamp = timeStamp();
  657. // 构建请求头
  658. LinkedHashMap<String, String> header = new LinkedHashMap<String, String>();
  659. header.put("X-Tsign-Open-App-Id", appId);
  660. header.put("X-Tsign-Open-Auth-Mode", "Signature");
  661. header.put("X-Tsign-Open-Ca-Timestamp", String.valueOf(timeStamp));
  662. header.put("Accept", accept);
  663. header.put("Content-Type", contentType);
  664. header.put("X-Tsign-Open-Ca-Signature", reqSignature);
  665. header.put("Content-MD5", contentMD5);
  666. // 发送POST请求
  667. String result = HTTPHelper.sendPOST(postAllUrl, reqBodyData, header, "UTF-8");
  668. JSONObject resultObj = JSONObject.parseObject(result);
  669. System.out.println("请求返回信息: " + resultObj.toString());
  670. } catch (Exception e) {
  671. e.printStackTrace();
  672. String msg = MessageFormat.format("请求签名鉴权方式调用接口出现异常: {0}", e.getMessage());
  673. System.out.println(msg);
  674. }
  675. }
  676. private static Map getRowMap(boolean insertRow,Object... columnValues) {
  677. Map row = new HashMap();
  678. for (int i = 0; i < columnValues.length; i++) {
  679. row.put("column" + (i + 1), columnValues[i]);
  680. }
  681. Map result = new HashMap();
  682. result.put("row",row);
  683. result.put("insertRow",insertRow);
  684. return result;
  685. }
  686. /***
  687. * 基于文件发起签署
  688. */
  689. public static void createByFile(String appId, String appKey, String host,String fileId) {
  690. // 计算签名拼接的url
  691. String postUrl = "/v3/sign-flow/create-by-file";
  692. // 完整的请求地址
  693. String postAllUrl = host + postUrl;
  694. try {
  695. // 构建请求Body体
  696. JSONObject reqBodyObj = new JSONObject();
  697. //设置待签署文件信息
  698. Map docs = new HashMap();
  699. docs.put("fileId", fileId);
  700. reqBodyObj.put("docs", Arrays.asList(docs));
  701. //签署流程配置项
  702. Map signFlowConfig = new HashMap();
  703. signFlowConfig.put("signFlowTitle","测试文件发起签署流程");
  704. signFlowConfig.put("autoFinish",true);
  705. // signFlowConfig.put("notifyUrl","https://33d4c762.r23.cpolar.top/huagao/eqb/callback");//回调地址
  706. reqBodyObj.put("signFlowConfig",signFlowConfig);
  707. //签署方信息
  708. Map signer = new HashMap();
  709. signer.put("signerType",1);//签署方类型,0 - 个人,1 - 企业/机构,2 - 法定代表人,3 - 经办人
  710. Map orgSignerInfo = new HashMap();
  711. orgSignerInfo.put("orgId","04dd6dbad3d8456b9e551fc329c516d7");
  712. //企业/机构经办人信息
  713. Map transactorInfo = new HashMap();
  714. transactorInfo.put("psnId","c086503ade9a4a3aba83f69c07b14178");//test
  715. orgSignerInfo.put("transactorInfo",transactorInfo);
  716. signer.put("orgSignerInfo",orgSignerInfo);
  717. /*Map psnSignerInfo = new HashMap();
  718. psnSignerInfo.put("psnId","0fd3eb8b0c424b4e827bb3bf1fba62f3");//wzy
  719. signer.put("psnSignerInfo",psnSignerInfo);*/
  720. Map signField = new HashMap();
  721. signField.put("fileId",fileId);
  722. signField.put("customBizNum","ydformInstId");
  723. Map normalSignFieldConfig = new HashMap();
  724. normalSignFieldConfig.put("freeMode",true);
  725. /*normalSignFieldConfig.put("autoSign",true);
  726. normalSignFieldConfig.put("signFieldStyle",1);
  727. Map signFieldPosition = new HashMap();
  728. signFieldPosition.put("positionPage",1);
  729. normalSignFieldConfig.put("signFieldPosition",signFieldPosition);*/
  730. signField.put("normalSignFieldConfig",normalSignFieldConfig);
  731. signer.put("signFields",Arrays.asList(signField));
  732. reqBodyObj.put("signers",Arrays.asList(signer));
  733. //reqBodyObj.put("convertToHTML", "false");
  734. // 请求Body体数据
  735. String reqBodyData = reqBodyObj.toString();
  736. // 对请求Body体内的数据计算ContentMD5
  737. String contentMD5 = doContentMD5(reqBodyData);
  738. System.out.println("请求body数据:"+reqBodyData);
  739. System.out.println("body的md5值:"+ contentMD5);
  740. // 构建待签名字符串
  741. String method = "POST";
  742. String accept = "*/*";
  743. String contentType = "application/json";
  744. String url = postUrl;
  745. String date = "";
  746. String headers = "";
  747. StringBuffer sb = new StringBuffer();
  748. sb.append(method).append("\n").append(accept).append("\n").append(contentMD5).append("\n")
  749. .append(contentType).append("\n").append(date).append("\n");
  750. if ("".equals(headers)) {
  751. sb.append(headers).append(url);
  752. } else {
  753. sb.append(headers).append("\n").append(url);
  754. }
  755. // 构建参与请求签名计算的明文
  756. String plaintext = sb.toString();
  757. // 计算请求签名值
  758. String reqSignature = doSignatureBase64(plaintext, appKey);
  759. System.out.println("计算请求签名值:"+reqSignature);
  760. // 获取时间戳(精确到毫秒)
  761. long timeStamp = timeStamp();
  762. // 构建请求头
  763. LinkedHashMap<String, String> header = new LinkedHashMap<String, String>();
  764. header.put("X-Tsign-Open-App-Id", appId);
  765. header.put("X-Tsign-Open-Auth-Mode", "Signature");
  766. header.put("X-Tsign-Open-Ca-Timestamp", String.valueOf(timeStamp));
  767. header.put("Accept", accept);
  768. header.put("Content-Type", contentType);
  769. header.put("X-Tsign-Open-Ca-Signature", reqSignature);
  770. header.put("Content-MD5", contentMD5);
  771. // 发送POST请求
  772. String result = HTTPHelper.sendPOST(postAllUrl, reqBodyData, header, "UTF-8");
  773. JSONObject resultObj = JSONObject.parseObject(result);
  774. System.out.println("请求返回信息: " + resultObj.toString());
  775. } catch (Exception e) {
  776. e.printStackTrace();
  777. String msg = MessageFormat.format("请求签名鉴权方式调用接口出现异常: {0}", e.getMessage());
  778. System.out.println(msg);
  779. }
  780. }
  781. /**
  782. * 查询机构认证信息
  783. */
  784. public static void getOrgInfo(String appId, String appKey, String host,String orgName) {
  785. try {
  786. // 计算签名拼接的url
  787. //urlencode转码
  788. String orgNameEncode = URLEncoder.encode(orgName,"UTF-8");
  789. String getUrl = "/v3/organizations/identity-info?orgName="+orgName;
  790. // 完整的请求地址
  791. String getAllUrl = host + "/v3/organizations/identity-info?orgName="+orgNameEncode;
  792. // GET请求时ContentMD5为""
  793. String contentMD5 = "";
  794. // 构建待签名字符串
  795. String method = "GET";
  796. String accept = "*/*";
  797. String contentType = "application/json; charset=UTF-8";
  798. String url = getUrl;
  799. String date = "";
  800. String headers = "";
  801. StringBuffer sb = new StringBuffer();
  802. sb.append(method).append("\n").append(accept).append("\n").append(contentMD5).append("\n")
  803. .append(contentType).append("\n").append(date).append("\n");
  804. if ("".equals(headers)) {
  805. sb.append(headers).append(url);
  806. } else {
  807. sb.append(headers).append("\n").append(url);
  808. }
  809. // 构建参与请求签名计算的明文
  810. String plaintext = sb.toString();
  811. // 计算请求签名值
  812. String reqSignature = doSignatureBase64(plaintext, appKey);
  813. System.out.println("计算请求签名值:"+ reqSignature);
  814. // 获取时间戳(精确到毫秒)
  815. long timeStamp = timeStamp();
  816. // 构建请求头
  817. LinkedHashMap<String, String> header = new LinkedHashMap<String, String>();
  818. header.put("X-Tsign-Open-App-Id", appId);
  819. header.put("X-Tsign-Open-Auth-Mode", "Signature");
  820. header.put("X-Tsign-Open-Ca-Signature", reqSignature);
  821. header.put("X-Tsign-Open-Ca-Timestamp", String.valueOf(timeStamp));
  822. header.put("Accept", accept);
  823. header.put("Content-Type", contentType);
  824. header.put("Content-MD5", contentMD5);
  825. System.out.println(header);
  826. HashMap<String, Object> query = new HashMap<String, Object>();
  827. // query.put("orgName", orgName);
  828. // query.put("orgIDCardType", "CRED_ORG_USCC");
  829. // 发送GET请求
  830. String result = HTTPHelper.sendGet(getAllUrl, query, header, "UTF-8");
  831. JSONObject resultObj = JSONObject.parseObject(result);
  832. System.out.println("请求返回信息: " + resultObj.toString());
  833. } catch (Exception e) {
  834. e.printStackTrace();
  835. String msg = MessageFormat.format("请求签名鉴权方式调用接口出现异常: {0}", e.getMessage());
  836. System.out.println(msg);
  837. }
  838. }
  839. /**
  840. * 查询企业用户授权详情
  841. */
  842. public static void getOrgUserInfo(String appId, String appKey, String host,String orgId) {
  843. // 计算签名拼接的url
  844. String getUrl = "/v3/organizations/"+orgId+"/authorized-info";
  845. // 完整的请求地址
  846. String getAllUrl = host + getUrl;
  847. try {
  848. // GET请求时ContentMD5为""
  849. String contentMD5 = "";
  850. // 构建待签名字符串
  851. String method = "GET";
  852. String accept = "*/*";
  853. String contentType = "application/json; charset=UTF-8";
  854. String url = getUrl;
  855. String date = "";
  856. String headers = "";
  857. StringBuffer sb = new StringBuffer();
  858. sb.append(method).append("\n").append(accept).append("\n").append(contentMD5).append("\n")
  859. .append(contentType).append("\n").append(date).append("\n");
  860. if ("".equals(headers)) {
  861. sb.append(headers).append(url);
  862. } else {
  863. sb.append(headers).append("\n").append(url);
  864. }
  865. // 构建参与请求签名计算的明文
  866. String plaintext = sb.toString();
  867. // 计算请求签名值
  868. String reqSignature = doSignatureBase64(plaintext, appKey);
  869. System.out.println("计算请求签名值:"+ reqSignature);
  870. // 获取时间戳(精确到毫秒)
  871. long timeStamp = timeStamp();
  872. // 构建请求头
  873. LinkedHashMap<String, String> header = new LinkedHashMap<String, String>();
  874. header.put("X-Tsign-Open-App-Id", appId);
  875. header.put("X-Tsign-Open-Auth-Mode", "Signature");
  876. header.put("X-Tsign-Open-Ca-Signature", reqSignature);
  877. header.put("X-Tsign-Open-Ca-Timestamp", String.valueOf(timeStamp));
  878. header.put("Accept", accept);
  879. header.put("Content-Type", contentType);
  880. header.put("Content-MD5", contentMD5);
  881. HashMap<String, Object> query = new HashMap<String, Object>();
  882. // query.put("orgName", orgName);
  883. // query.put("orgIDCardType", "CRED_ORG_USCC");
  884. // 发送GET请求
  885. String result = HTTPHelper.sendGet(getAllUrl, query, header, "UTF-8");
  886. JSONObject resultObj = JSONObject.parseObject(result);
  887. System.out.println("请求返回信息: " + resultObj.toString());
  888. } catch (Exception e) {
  889. e.printStackTrace();
  890. String msg = MessageFormat.format("请求签名鉴权方式调用接口出现异常: {0}", e.getMessage());
  891. System.out.println(msg);
  892. }
  893. }
  894. /***
  895. * 查询企业成员列表
  896. */
  897. public static void getOrgMember(String appId, String appKey, String host,String orgId) {
  898. // 计算签名拼接的url
  899. String getUrl = "/v3/organizations/"+orgId+"/member-list?pageNum=1&pageSize=100";
  900. // 完整的请求地址
  901. String getAllUrl = host + getUrl;
  902. try {
  903. // GET请求时ContentMD5为""
  904. String contentMD5 = "";
  905. // 构建待签名字符串
  906. String method = "GET";
  907. String accept = "*/*";
  908. String contentType = "application/json; charset=UTF-8";
  909. String url = getUrl;
  910. String date = "";
  911. String headers = "";
  912. StringBuffer sb = new StringBuffer();
  913. sb.append(method).append("\n").append(accept).append("\n").append(contentMD5).append("\n")
  914. .append(contentType).append("\n").append(date).append("\n");
  915. if ("".equals(headers)) {
  916. sb.append(headers).append(url);
  917. } else {
  918. sb.append(headers).append("\n").append(url);
  919. }
  920. // 构建参与请求签名计算的明文
  921. String plaintext = sb.toString();
  922. // 计算请求签名值
  923. String reqSignature = doSignatureBase64(plaintext, appKey);
  924. System.out.println("计算请求签名值:"+ reqSignature);
  925. // 获取时间戳(精确到毫秒)
  926. long timeStamp = timeStamp();
  927. // 构建请求头
  928. LinkedHashMap<String, String> header = new LinkedHashMap<String, String>();
  929. header.put("X-Tsign-Open-App-Id", appId);
  930. header.put("X-Tsign-Open-Auth-Mode", "Signature");
  931. header.put("X-Tsign-Open-Ca-Signature", reqSignature);
  932. header.put("X-Tsign-Open-Ca-Timestamp", String.valueOf(timeStamp));
  933. header.put("Accept", accept);
  934. header.put("Content-Type", contentType);
  935. header.put("Content-MD5", contentMD5);
  936. HashMap<String, Object> query = new HashMap<String, Object>();
  937. // query.put("orgIDCardNum", "9100*****0004");
  938. // query.put("orgIDCardType", "CRED_ORG_USCC");
  939. // 发送GET请求
  940. String result = HTTPHelper.sendGet(getAllUrl, query, header, "UTF-8");
  941. JSONObject resultObj = JSONObject.parseObject(result);
  942. System.out.println("请求返回信息: " + resultObj.toString());
  943. } catch (Exception e) {
  944. e.printStackTrace();
  945. String msg = MessageFormat.format("请求签名鉴权方式调用接口出现异常: {0}", e.getMessage());
  946. System.out.println(msg);
  947. }
  948. }
  949. /***
  950. * 查询个人认证信息
  951. */
  952. public static void getPersonInfo(String appId, String appKey, String host,String psnAccount) {
  953. // 计算签名拼接的url
  954. String getUrl = "/v3/persons/identity-info"+"?psnAccount="+psnAccount;
  955. // 完整的请求地址
  956. String getAllUrl = host + getUrl;
  957. try {
  958. // GET请求时ContentMD5为""
  959. String contentMD5 = "";
  960. // 构建待签名字符串
  961. String method = "GET";
  962. String accept = "*/*";
  963. String contentType = "application/json; charset=UTF-8";
  964. String url = getUrl;
  965. String date = "";
  966. String headers = "";
  967. StringBuffer sb = new StringBuffer();
  968. sb.append(method).append("\n").append(accept).append("\n").append(contentMD5).append("\n")
  969. .append(contentType).append("\n").append(date).append("\n");
  970. if ("".equals(headers)) {
  971. sb.append(headers).append(url);
  972. } else {
  973. sb.append(headers).append("\n").append(url);
  974. }
  975. // 构建参与请求签名计算的明文
  976. String plaintext = sb.toString();
  977. // 计算请求签名值
  978. String reqSignature = doSignatureBase64(plaintext, appKey);
  979. System.out.println("计算请求签名值:"+ reqSignature);
  980. // 获取时间戳(精确到毫秒)
  981. long timeStamp = timeStamp();
  982. // 构建请求头
  983. LinkedHashMap<String, String> header = new LinkedHashMap<String, String>();
  984. header.put("X-Tsign-Open-App-Id", appId);
  985. header.put("X-Tsign-Open-Auth-Mode", "Signature");
  986. header.put("X-Tsign-Open-Ca-Signature", reqSignature);
  987. header.put("X-Tsign-Open-Ca-Timestamp", String.valueOf(timeStamp));
  988. header.put("Accept", accept);
  989. header.put("Content-Type", contentType);
  990. header.put("Content-MD5", contentMD5);
  991. HashMap<String, Object> query = new HashMap<String, Object>();
  992. // query.put("orgIDCardNum", "9100*****0004");
  993. // query.put("orgIDCardType", "CRED_ORG_USCC");
  994. // 发送GET请求
  995. String result = HTTPHelper.sendGet(getAllUrl, query, header, "UTF-8");
  996. JSONObject resultObj = JSONObject.parseObject(result);
  997. System.out.println("请求返回信息: " + resultObj.toString());
  998. } catch (Exception e) {
  999. e.printStackTrace();
  1000. String msg = MessageFormat.format("请求签名鉴权方式调用接口出现异常: {0}", e.getMessage());
  1001. System.out.println(msg);
  1002. }
  1003. }
  1004. /***
  1005. * 下载已签署文件及附属材料
  1006. */
  1007. public static void downloadFile(String appId, String appKey, String host,String signFlowId) {
  1008. // 计算签名拼接的url
  1009. String postUrl = "/v3/sign-flow/" + signFlowId + "/file-download-url";
  1010. // 完整的请求地址
  1011. String postAllUrl = host + postUrl;
  1012. try {
  1013. // 构建请求Body体
  1014. JSONObject reqBodyObj = new JSONObject();
  1015. // 请求Body体数据
  1016. String reqBodyData = reqBodyObj.toString();
  1017. // 对请求Body体内的数据计算ContentMD5
  1018. String contentMD5 = doContentMD5(reqBodyData);
  1019. System.out.println("请求body数据:"+reqBodyData);
  1020. System.out.println("body的md5值:"+ contentMD5);
  1021. // 构建待签名字符串
  1022. String method = "POST";
  1023. String accept = "*/*";
  1024. String contentType = "application/json";
  1025. String url = postUrl;
  1026. String date = "";
  1027. String headers = "";
  1028. StringBuffer sb = new StringBuffer();
  1029. sb.append(method).append("\n").append(accept).append("\n").append(contentMD5).append("\n")
  1030. .append(contentType).append("\n").append(date).append("\n");
  1031. if ("".equals(headers)) {
  1032. sb.append(headers).append(url);
  1033. } else {
  1034. sb.append(headers).append("\n").append(url);
  1035. }
  1036. // 构建参与请求签名计算的明文
  1037. String plaintext = sb.toString();
  1038. // 计算请求签名值
  1039. String reqSignature = doSignatureBase64(plaintext, appKey);
  1040. System.out.println("计算请求签名值:"+reqSignature);
  1041. // 获取时间戳(精确到毫秒)
  1042. long timeStamp = timeStamp();
  1043. // 构建请求头
  1044. LinkedHashMap<String, String> header = new LinkedHashMap<String, String>();
  1045. header.put("X-Tsign-Open-App-Id", appId);
  1046. header.put("X-Tsign-Open-Auth-Mode", "Signature");
  1047. header.put("X-Tsign-Open-Ca-Timestamp", String.valueOf(timeStamp));
  1048. header.put("Accept", accept);
  1049. header.put("Content-Type", contentType);
  1050. header.put("X-Tsign-Open-Ca-Signature", reqSignature);
  1051. header.put("Content-MD5", contentMD5);
  1052. // 发送POST请求
  1053. String result = HTTPHelper.sendPOST(postAllUrl, reqBodyData, header, "UTF-8");
  1054. JSONObject resultObj = JSONObject.parseObject(result);
  1055. System.out.println("请求返回信息: " + resultObj.toString());
  1056. } catch (Exception e) {
  1057. e.printStackTrace();
  1058. String msg = MessageFormat.format("请求签名鉴权方式调用接口出现异常: {0}", e.getMessage());
  1059. System.out.println(msg);
  1060. }
  1061. }
  1062. }