EqbTest.java 58 KB

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