EqbTest.java 57 KB

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