| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400 |
- package com.malk.huagao;
- import java.io.UnsupportedEncodingException;
- import java.net.URLEncoder;
- import java.security.InvalidKeyException;
- import java.security.MessageDigest;
- import java.security.NoSuchAlgorithmException;
- import java.text.MessageFormat;
- import java.util.*;
- import javax.crypto.Mac;
- import javax.crypto.spec.SecretKeySpec;
- import com.malk.huagao.utils.HTTPHelper;
- import org.apache.commons.codec.binary.Base64;
- import com.alibaba.fastjson.JSONObject;
- public class EqbTest {
- public static void main(String[] args) {
- // 应用ID
- /*String appId = "7439093376";
- // 应用密钥(AppSecret)
- String appKey = "0589e55190dfd861d29a2313ce0b6f56";
- // e签宝接口调用域名(模拟环境)
- String host = "https://smlopenapi.esign.cn";*/
- // 应用ID
- String appId = "5112033166";
- // 应用密钥(AppSecret)
- String appKey = "c0641a1f648a68a7014be5a490f0159a";
- // e签宝接口调用域名(正式环境)
- String host = "https://openapi.esign.cn";
- //步骤1:上传本地文件并转成HTML格式
- //1.1获取文件上传地址
- // getFileUploadUrl(appId,appKey,host);
- //1.2上传文件流 postman
- //步骤2:查看文件上传状态及详情
- // getFileInfo(appId,appKey,host,"1c2ede25c6454ad4af8bcba3dd9beb35");
- //步骤3:获取制作合同模板页面链接
- // getDocCreateUrl(appId,appKey,host,"418d535ab69f4c1fbe01931b8b74f82f");
- //测试环境模板id ef7d3bcee4254e67944d53db6a1f76eb
- //正式环境模板id 经销商模板:6a23f76742d74b858cdc2259a5d5c32f
- //正式环境模板id 其他客户模板:8b7fef2257bc4093ada330dbf7c874c5
- //正式环境模板id 采购订单模板:a0827f8946994ebfbed7f44e6b8a6ed8
- //步骤4:制作含动态表格控件的HTML模板 访问【获取制作合同模板页面】接口返回的创建文件模板页面链接(docTemplateCreateUrl参数值),并在页面中拖动【动态表格】控件来制作模板,若链接失效,调用【获取编辑合同模板页面】接口
- getDocTemplateEditUrl(appId,appKey,host,"8b7fef2257bc4093ada330dbf7c874c5");
- //步骤5:获取 HTML 模板中控件ID和控件Key
- // getDocTemplateInfo(appId,appKey,host,"6a23f76742d74b858cdc2259a5d5c32f");
- //步骤6:构造数据并填充模板生成文件
- //6.1填写模板生成文件
- // createByDocTemplate(appId,appKey,host,"6a23f76742d74b858cdc2259a5d5c32f");
- //6.2开发者使用【查询文件上传状态】接口获取填充后文件链接,通过此链接可查看填充后文件效果
- // getFileInfo(appId,appKey,host,"28e65af0dd3a4f989da68d14bcd06373");
- //基于文件发起签署
- // createByFile(appId,appKey,host,"1c2ede25c6454ad4af8bcba3dd9beb35");
- //查询签署流程详情
- // getSignFlowDetail(appId,appKey,host,"62dc01292d684653bae341adb014b4d0");
- //下载签署文件
- // downloadFile(appId,appKey,host,"9dbc4ddfbaba477084f046fc095032e6");
- //查询机构认证信息(暂不使用 鉴权失败)
- // getOrgInfo(appId,appKey,host,"宁波华高信息科技有限公司");
- // getOrgUserInfo(appId,appKey,host,"00739a62731c498a8ecf477bf79efc54");
- // getOrgMember(appId,appKey,host,"a5ec8fb7d8cc4276bd486824df0ec640");
- //获取签署地址
- // getSignUrl(appId,appKey,host,"10fa0c28225a45a4ab31d8a7b98cb601","0fd3eb8b0c424b4e827bb3bf1fba62f3");
- // 请求签名鉴权-POST请求
- // testPost(appId, appKey, host);
- // 请求签名鉴权-GET请求
- // String signFlowId = "e622498****ebf72d57dbb";
- // testGet(appId, appKey, host, signFlowId);
- }
- /***
- * 获取文件上传地址
- */
- public static void getFileUploadUrl(String appId, String appKey, String host) {
- // 计算签名拼接的url
- String postUrl = "/v3/files/file-upload-url";
- // 完整的请求地址
- String postAllUrl = host + postUrl;
- try {
- // 构建请求Body体
- JSONObject reqBodyObj = new JSONObject();
- reqBodyObj.put("contentMd5", "iyxiNd3iJ7A+QFE9fAUqvQ==");
- reqBodyObj.put("contentType", "application/octet-stream");
- // reqBodyObj.put("convertToPDF", "true");
- reqBodyObj.put("convertToHTML", true);
- reqBodyObj.put("fileName", "其他客户销售合同.docx");
- reqBodyObj.put("fileSize", "28246 ");
- //reqBodyObj.put("convertToHTML", "false");
- // 请求Body体数据
- String reqBodyData = reqBodyObj.toString();
- // 对请求Body体内的数据计算ContentMD5
- String contentMD5 = doContentMD5(reqBodyData);
- System.out.println("请求body数据:"+reqBodyData);
- System.out.println("body的md5值:"+ contentMD5);
- // 构建待签名字符串
- String method = "POST";
- String accept = "*/*";
- String contentType = "application/json";
- String url = postUrl;
- String date = "";
- String headers = "";
- StringBuffer sb = new StringBuffer();
- sb.append(method).append("\n").append(accept).append("\n").append(contentMD5).append("\n")
- .append(contentType).append("\n").append(date).append("\n");
- if ("".equals(headers)) {
- sb.append(headers).append(url);
- } else {
- sb.append(headers).append("\n").append(url);
- }
- // 构建参与请求签名计算的明文
- String plaintext = sb.toString();
- // 计算请求签名值
- String reqSignature = doSignatureBase64(plaintext, appKey);
- System.out.println("计算请求签名值:"+reqSignature);
- // 获取时间戳(精确到毫秒)
- long timeStamp = timeStamp();
- // 构建请求头
- LinkedHashMap<String, String> header = new LinkedHashMap<String, String>();
- header.put("X-Tsign-Open-App-Id", appId);
- header.put("X-Tsign-Open-Auth-Mode", "Signature");
- header.put("X-Tsign-Open-Ca-Timestamp", String.valueOf(timeStamp));
- header.put("Accept", accept);
- header.put("Content-Type", contentType);
- header.put("X-Tsign-Open-Ca-Signature", reqSignature);
- header.put("Content-MD5", contentMD5);
- // 发送POST请求
- String result = HTTPHelper.sendPOST(postAllUrl, reqBodyData, header, "UTF-8");
- JSONObject resultObj = JSONObject.parseObject(result);
- System.out.println("请求返回信息: " + resultObj.toString());
- } catch (Exception e) {
- e.printStackTrace();
- String msg = MessageFormat.format("请求签名鉴权方式调用接口出现异常: {0}", e.getMessage());
- System.out.println(msg);
- }
- }
- /**
- * 查询文件上传状态
- */
- public static void getFileInfo(String appId, String appKey, String host,String fileId) {
- // 计算签名拼接的url
- String getUrl = "/v3/files/"+fileId;
- // 完整的请求地址
- String getAllUrl = host + getUrl;
- try {
- // GET请求时ContentMD5为""
- String contentMD5 = "";
- // 构建待签名字符串
- String method = "GET";
- String accept = "*/*";
- String contentType = "application/json; charset=UTF-8";
- String url = getUrl;
- String date = "";
- String headers = "";
- StringBuffer sb = new StringBuffer();
- sb.append(method).append("\n").append(accept).append("\n").append(contentMD5).append("\n")
- .append(contentType).append("\n").append(date).append("\n");
- if ("".equals(headers)) {
- sb.append(headers).append(url);
- } else {
- sb.append(headers).append("\n").append(url);
- }
- // 构建参与请求签名计算的明文
- String plaintext = sb.toString();
- // 计算请求签名值
- String reqSignature = doSignatureBase64(plaintext, appKey);
- System.out.println("计算请求签名值:"+ reqSignature);
- // 获取时间戳(精确到毫秒)
- long timeStamp = timeStamp();
- // 构建请求头
- LinkedHashMap<String, String> header = new LinkedHashMap<String, String>();
- header.put("X-Tsign-Open-App-Id", appId);
- header.put("X-Tsign-Open-Auth-Mode", "Signature");
- header.put("X-Tsign-Open-Ca-Signature", reqSignature);
- header.put("X-Tsign-Open-Ca-Timestamp", String.valueOf(timeStamp));
- header.put("Accept", accept);
- header.put("Content-Type", contentType);
- header.put("Content-MD5", contentMD5);
- HashMap<String, Object> query = new HashMap<String, Object>();
- // query.put("orgIDCardNum", "9100*****0004");
- // query.put("orgIDCardType", "CRED_ORG_USCC");
- // 发送GET请求
- String result = HTTPHelper.sendGet(getAllUrl, query, header, "UTF-8");
- JSONObject resultObj = JSONObject.parseObject(result);
- System.out.println("请求返回信息: " + resultObj.toString());
- } catch (Exception e) {
- e.printStackTrace();
- String msg = MessageFormat.format("请求签名鉴权方式调用接口出现异常: {0}", e.getMessage());
- System.out.println(msg);
- }
- }
- /***
- * 获取制作合同模板页面
- */
- public static void getDocCreateUrl(String appId, String appKey, String host,String fileId) {
- // 计算签名拼接的url
- String postUrl = "/v3/doc-templates/doc-template-create-url";
- // 完整的请求地址
- String postAllUrl = host + postUrl;
- try {
- // 构建请求Body体
- JSONObject reqBodyObj = new JSONObject();
- reqBodyObj.put("docTemplateName", "其他客户销售合同");
- reqBodyObj.put("docTemplateType", 1);
- reqBodyObj.put("fileId", fileId);
- // reqBodyObj.put("hiddenOriginComponents", "false");
- // 请求Body体数据
- String reqBodyData = reqBodyObj.toString();
- // 对请求Body体内的数据计算ContentMD5
- String contentMD5 = doContentMD5(reqBodyData);
- System.out.println("请求body数据:"+reqBodyData);
- System.out.println("body的md5值:"+ contentMD5);
- // 构建待签名字符串
- String method = "POST";
- String accept = "*/*";
- String contentType = "application/json";
- String url = postUrl;
- String date = "";
- String headers = "";
- StringBuffer sb = new StringBuffer();
- sb.append(method).append("\n").append(accept).append("\n").append(contentMD5).append("\n")
- .append(contentType).append("\n").append(date).append("\n");
- if ("".equals(headers)) {
- sb.append(headers).append(url);
- } else {
- sb.append(headers).append("\n").append(url);
- }
- // 构建参与请求签名计算的明文
- String plaintext = sb.toString();
- // 计算请求签名值
- String reqSignature = doSignatureBase64(plaintext, appKey);
- System.out.println("计算请求签名值:"+reqSignature);
- // 获取时间戳(精确到毫秒)
- long timeStamp = timeStamp();
- // 构建请求头
- LinkedHashMap<String, String> header = new LinkedHashMap<String, String>();
- header.put("X-Tsign-Open-App-Id", appId);
- header.put("X-Tsign-Open-Auth-Mode", "Signature");
- header.put("X-Tsign-Open-Ca-Timestamp", String.valueOf(timeStamp));
- header.put("Accept", accept);
- header.put("Content-Type", contentType);
- header.put("X-Tsign-Open-Ca-Signature", reqSignature);
- header.put("Content-MD5", contentMD5);
- // 发送POST请求
- String result = HTTPHelper.sendPOST(postAllUrl, reqBodyData, header, "UTF-8");
- JSONObject resultObj = JSONObject.parseObject(result);
- System.out.println("请求返回信息: " + resultObj.toString());
- } catch (Exception e) {
- e.printStackTrace();
- String msg = MessageFormat.format("请求签名鉴权方式调用接口出现异常: {0}", e.getMessage());
- System.out.println(msg);
- }
- }
- /**
- * 查询合同模板中控件详情
- */
- public static void getDocTemplateInfo(String appId, String appKey, String host,String docTemplateId) {
- // 计算签名拼接的url
- String getUrl = "/v3/doc-templates/"+docTemplateId;
- // 完整的请求地址
- String getAllUrl = host + getUrl;
- try {
- // GET请求时ContentMD5为""
- String contentMD5 = "";
- // 构建待签名字符串
- String method = "GET";
- String accept = "*/*";
- String contentType = "application/json; charset=UTF-8";
- String url = getUrl;
- String date = "";
- String headers = "";
- StringBuffer sb = new StringBuffer();
- sb.append(method).append("\n").append(accept).append("\n").append(contentMD5).append("\n")
- .append(contentType).append("\n").append(date).append("\n");
- if ("".equals(headers)) {
- sb.append(headers).append(url);
- } else {
- sb.append(headers).append("\n").append(url);
- }
- // 构建参与请求签名计算的明文
- String plaintext = sb.toString();
- // 计算请求签名值
- String reqSignature = doSignatureBase64(plaintext, appKey);
- System.out.println("计算请求签名值:"+ reqSignature);
- // 获取时间戳(精确到毫秒)
- long timeStamp = timeStamp();
- // 构建请求头
- LinkedHashMap<String, String> header = new LinkedHashMap<String, String>();
- header.put("X-Tsign-Open-App-Id", appId);
- header.put("X-Tsign-Open-Auth-Mode", "Signature");
- header.put("X-Tsign-Open-Ca-Signature", reqSignature);
- header.put("X-Tsign-Open-Ca-Timestamp", String.valueOf(timeStamp));
- header.put("Accept", accept);
- header.put("Content-Type", contentType);
- header.put("Content-MD5", contentMD5);
- HashMap<String, Object> query = new HashMap<String, Object>();
- // query.put("orgIDCardNum", "9100*****0004");
- // query.put("orgIDCardType", "CRED_ORG_USCC");
- // 发送GET请求
- String result = HTTPHelper.sendGet(getAllUrl, query, header, "UTF-8");
- JSONObject resultObj = JSONObject.parseObject(result);
- System.out.println("请求返回信息: " + resultObj.toString());
- } catch (Exception e) {
- e.printStackTrace();
- String msg = MessageFormat.format("请求签名鉴权方式调用接口出现异常: {0}", e.getMessage());
- System.out.println(msg);
- }
- }
- /***
- * 填写模板生成文件
- */
- public static void createByDocTemplate(String appId, String appKey, String host,String docTemplateId) {
- // 计算签名拼接的url
- String postUrl = "/v3/files/create-by-doc-template";
- // 完整的请求地址
- String postAllUrl = host + postUrl;
- try {
- List<Map> components = new ArrayList<>();
- //订购单编号
- Map component = new HashMap();
- component.put("componentKey", "dgdbh");
- component.put("componentValue", "ABC0001");
- //需方
- Map component2 = new HashMap();
- component2.put("componentKey", "xf");
- component2.put("componentValue", "测试XX公司");
- //需方地址
- Map component3 = new HashMap();
- component3.put("componentKey", "xfdz");
- component3.put("componentValue", "测试地址");
- //需方电话
- Map component4 = new HashMap();
- component4.put("componentKey", "xfdh");
- component4.put("componentValue", "15900010001");
- //供方电话
- Map component5 = new HashMap();
- component5.put("componentKey", "gfdh");
- component5.put("componentValue", "15900010002");
- //订单日期
- Map component6 = new HashMap();
- component6.put("componentKey", "ddrq");
- component6.put("componentValue", "2025年12月12日");
- //供货框架协议编号
- Map component7 = new HashMap();
- component7.put("componentKey", "ghkjxybh");
- component7.put("componentValue", "CBA0001");
- //销售合同明细
- List<Map> xshtmx = new ArrayList<>();
- xshtmx.add(getRowMap(false));
- xshtmx.add(getRowMap(false,"1","产品1","型号1","单位1","1.12","1.10","1"));
- xshtmx.add(getRowMap(true,"2","产品2","型号2","单位2","2.12","2.10","2"));
- xshtmx.add(getRowMap(true,"3","产品3","型号3","单位3","3.12","4.10","3"));
- xshtmx.add(getRowMap(true,"4","产品4","型号4","单位4","4.12","4.10","4"));
- xshtmx.add(getRowMap(true,"5","产品5","型号5","单位5","5.12","5.10","5"));
- xshtmx.add(getRowMap(true,"6","产品6","型号6","单位6","6.12","6.10","6"));
- xshtmx.add(getRowMap(true,"7","产品7","型号7","单位7","7.12","7.10","7"));
- xshtmx.add(getRowMap(true,"8","产品8","型号8","单位8","8.12","8.10","8"));
- Map component8 = new HashMap();
- component8.put("componentKey", "xshtmx");
- component8.put("componentValue", JSONObject.toJSONString(xshtmx));
- //金额合计
- Map component9 = new HashMap();
- component9.put("componentKey", "jehj");
- component9.put("componentValue", "100.00");
- //金额合计大写
- Map component10 = new HashMap();
- component10.put("componentKey", "jehjdx");
- component10.put("componentValue", "壹佰");
- //先款后货
- Map component11 = new HashMap();
- component11.put("componentKey", "xkhh");
- component11.put("componentValue", true);
- //交货地址
- List<Map> jhdz = new ArrayList<>();
- jhdz.add(getRowMap(false));
- jhdz.add(getRowMap(false,"1","地址1","收货人1","联系电话1"));
- jhdz.add(getRowMap(true,"2","地址2","收货人2","联系电话2"));
- jhdz.add(getRowMap(true,"3","地址3","收货人3","联系电话3"));
- jhdz.add(getRowMap(true,"4","地址4","收货人4","联系电话4"));
- Map component12 = new HashMap();
- component12.put("componentKey", "jhdz");
- component12.put("componentValue", JSONObject.toJSONString(jhdz));
- //需方纳税人识别号
- Map component13 = new HashMap();
- component13.put("componentKey", "xfnsrsbh");
- component13.put("componentValue", "SDAJAIDHAIWJ1231");
- //需方开户银行
- Map component14 = new HashMap();
- component14.put("componentKey", "xfkhyh");
- component14.put("componentValue", "中国银行");
- //需方账号
- Map component15 = new HashMap();
- component15.put("componentKey", "xfzh");
- component15.put("componentValue", "12318231726371231");
- //需方2
- Map component16 = new HashMap();
- component16.put("componentKey", "xf2");
- component16.put("componentValue", "测试XX公司");
- //需方地址2
- Map component17 = new HashMap();
- component17.put("componentKey", "xfdz2");
- component17.put("componentValue", "测试地址");
- components.add(component);
- components.add(component2);
- components.add(component3);
- components.add(component4);
- components.add(component5);
- components.add(component6);
- components.add(component7);
- components.add(component8);
- components.add(component9);
- components.add(component10);
- components.add(component11);
- components.add(component12);
- components.add(component13);
- components.add(component14);
- components.add(component15);
- components.add(component16);
- components.add(component17);
- // 构建请求Body体
- JSONObject reqBodyObj = new JSONObject();
- reqBodyObj.put("docTemplateId", docTemplateId);
- reqBodyObj.put("fileName", "测试经销商合同.pdf");
- reqBodyObj.put("components", components);
- // 请求Body体数据
- String reqBodyData = reqBodyObj.toString();
- // 对请求Body体内的数据计算ContentMD5
- String contentMD5 = doContentMD5(reqBodyData);
- System.out.println("请求body数据:"+reqBodyData);
- System.out.println("body的md5值:"+ contentMD5);
- // 构建待签名字符串
- String method = "POST";
- String accept = "*/*";
- String contentType = "application/json";
- String url = postUrl;
- String date = "";
- String headers = "";
- StringBuffer sb = new StringBuffer();
- sb.append(method).append("\n").append(accept).append("\n").append(contentMD5).append("\n")
- .append(contentType).append("\n").append(date).append("\n");
- if ("".equals(headers)) {
- sb.append(headers).append(url);
- } else {
- sb.append(headers).append("\n").append(url);
- }
- // 构建参与请求签名计算的明文
- String plaintext = sb.toString();
- // 计算请求签名值
- String reqSignature = doSignatureBase64(plaintext, appKey);
- System.out.println("计算请求签名值:"+reqSignature);
- // 获取时间戳(精确到毫秒)
- long timeStamp = timeStamp();
- // 构建请求头
- LinkedHashMap<String, String> header = new LinkedHashMap<String, String>();
- header.put("X-Tsign-Open-App-Id", appId);
- header.put("X-Tsign-Open-Auth-Mode", "Signature");
- header.put("X-Tsign-Open-Ca-Timestamp", String.valueOf(timeStamp));
- header.put("Accept", accept);
- header.put("Content-Type", contentType);
- header.put("X-Tsign-Open-Ca-Signature", reqSignature);
- header.put("Content-MD5", contentMD5);
- // 发送POST请求
- String result = HTTPHelper.sendPOST(postAllUrl, reqBodyData, header, "UTF-8");
- JSONObject resultObj = JSONObject.parseObject(result);
- System.out.println("请求返回信息: " + resultObj.toString());
- } catch (Exception e) {
- e.printStackTrace();
- String msg = MessageFormat.format("请求签名鉴权方式调用接口出现异常: {0}", e.getMessage());
- System.out.println(msg);
- }
- }
- /***
- * 基于文件发起签署
- */
- public static void createByFile(String appId, String appKey, String host,String fileId) {
- // 计算签名拼接的url
- String postUrl = "/v3/sign-flow/create-by-file";
- // 完整的请求地址
- String postAllUrl = host + postUrl;
- try {
- // 构建请求Body体
- JSONObject reqBodyObj = new JSONObject();
- //设置待签署文件信息
- Map docs = new HashMap();
- docs.put("fileId", fileId);
- reqBodyObj.put("docs", Arrays.asList(docs));
- //签署流程配置项
- Map signFlowConfig = new HashMap();
- signFlowConfig.put("signFlowTitle","测试文件发起签署流程");
- signFlowConfig.put("autoFinish",true);
- signFlowConfig.put("notifyUrl","https://33d4c762.r23.cpolar.top/huagao/eqb/callback");//回调地址
- reqBodyObj.put("signFlowConfig",signFlowConfig);
- //签署方信息
- Map signer = new HashMap();
- signer.put("signerType",1);//签署方类型,0 - 个人,1 - 企业/机构,2 - 法定代表人,3 - 经办人
- Map orgSignerInfo = new HashMap();
- orgSignerInfo.put("orgId","a5ec8fb7d8cc4276bd486824df0ec640");
- //企业/机构经办人信息
- Map transactorInfo = new HashMap();
- transactorInfo.put("psnId","0fd3eb8b0c424b4e827bb3bf1fba62f3");//wzy
- orgSignerInfo.put("transactorInfo",transactorInfo);
- signer.put("orgSignerInfo",orgSignerInfo);
- /*Map psnSignerInfo = new HashMap();
- psnSignerInfo.put("psnId","0fd3eb8b0c424b4e827bb3bf1fba62f3");//wzy
- signer.put("psnSignerInfo",psnSignerInfo);*/
- Map signField = new HashMap();
- signField.put("fileId",fileId);
- signField.put("customBizNum","ydformInstId");
- Map normalSignFieldConfig = new HashMap();
- normalSignFieldConfig.put("freeMode",true);
- /*normalSignFieldConfig.put("autoSign",true);
- normalSignFieldConfig.put("signFieldStyle",1);
- Map signFieldPosition = new HashMap();
- signFieldPosition.put("positionPage",1);
- normalSignFieldConfig.put("signFieldPosition",signFieldPosition);*/
- signField.put("normalSignFieldConfig",normalSignFieldConfig);
- signer.put("signFields",Arrays.asList(signField));
- reqBodyObj.put("signers",Arrays.asList(signer));
- //reqBodyObj.put("convertToHTML", "false");
- // 请求Body体数据
- String reqBodyData = reqBodyObj.toString();
- // 对请求Body体内的数据计算ContentMD5
- String contentMD5 = doContentMD5(reqBodyData);
- System.out.println("请求body数据:"+reqBodyData);
- System.out.println("body的md5值:"+ contentMD5);
- // 构建待签名字符串
- String method = "POST";
- String accept = "*/*";
- String contentType = "application/json";
- String url = postUrl;
- String date = "";
- String headers = "";
- StringBuffer sb = new StringBuffer();
- sb.append(method).append("\n").append(accept).append("\n").append(contentMD5).append("\n")
- .append(contentType).append("\n").append(date).append("\n");
- if ("".equals(headers)) {
- sb.append(headers).append(url);
- } else {
- sb.append(headers).append("\n").append(url);
- }
- // 构建参与请求签名计算的明文
- String plaintext = sb.toString();
- // 计算请求签名值
- String reqSignature = doSignatureBase64(plaintext, appKey);
- System.out.println("计算请求签名值:"+reqSignature);
- // 获取时间戳(精确到毫秒)
- long timeStamp = timeStamp();
- // 构建请求头
- LinkedHashMap<String, String> header = new LinkedHashMap<String, String>();
- header.put("X-Tsign-Open-App-Id", appId);
- header.put("X-Tsign-Open-Auth-Mode", "Signature");
- header.put("X-Tsign-Open-Ca-Timestamp", String.valueOf(timeStamp));
- header.put("Accept", accept);
- header.put("Content-Type", contentType);
- header.put("X-Tsign-Open-Ca-Signature", reqSignature);
- header.put("Content-MD5", contentMD5);
- // 发送POST请求
- String result = HTTPHelper.sendPOST(postAllUrl, reqBodyData, header, "UTF-8");
- JSONObject resultObj = JSONObject.parseObject(result);
- System.out.println("请求返回信息: " + resultObj.toString());
- } catch (Exception e) {
- e.printStackTrace();
- String msg = MessageFormat.format("请求签名鉴权方式调用接口出现异常: {0}", e.getMessage());
- System.out.println(msg);
- }
- }
- /**
- * 查询机构认证信息
- */
- public static void getOrgInfo(String appId, String appKey, String host,String orgName) {
- try {
- // 计算签名拼接的url
- //urlencode转码
- String orgNameEncode = URLEncoder.encode(orgName,"UTF-8");
- String getUrl = "/v3/organizations/identity-info?orgName="+orgName;
- // 完整的请求地址
- String getAllUrl = host + "/v3/organizations/identity-info?orgName="+orgNameEncode;
- // GET请求时ContentMD5为""
- String contentMD5 = "";
- // 构建待签名字符串
- String method = "GET";
- String accept = "*/*";
- String contentType = "application/json; charset=UTF-8";
- String url = getUrl;
- String date = "";
- String headers = "";
- StringBuffer sb = new StringBuffer();
- sb.append(method).append("\n").append(accept).append("\n").append(contentMD5).append("\n")
- .append(contentType).append("\n").append(date).append("\n");
- if ("".equals(headers)) {
- sb.append(headers).append(url);
- } else {
- sb.append(headers).append("\n").append(url);
- }
- // 构建参与请求签名计算的明文
- String plaintext = sb.toString();
- // 计算请求签名值
- String reqSignature = doSignatureBase64(plaintext, appKey);
- System.out.println("计算请求签名值:"+ reqSignature);
- // 获取时间戳(精确到毫秒)
- long timeStamp = timeStamp();
- // 构建请求头
- LinkedHashMap<String, String> header = new LinkedHashMap<String, String>();
- header.put("X-Tsign-Open-App-Id", appId);
- header.put("X-Tsign-Open-Auth-Mode", "Signature");
- header.put("X-Tsign-Open-Ca-Signature", reqSignature);
- header.put("X-Tsign-Open-Ca-Timestamp", String.valueOf(timeStamp));
- header.put("Accept", accept);
- header.put("Content-Type", contentType);
- header.put("Content-MD5", contentMD5);
- System.out.println(header);
- HashMap<String, Object> query = new HashMap<String, Object>();
- // query.put("orgName", orgName);
- // query.put("orgIDCardType", "CRED_ORG_USCC");
- // 发送GET请求
- String result = HTTPHelper.sendGet(getAllUrl, query, header, "UTF-8");
- JSONObject resultObj = JSONObject.parseObject(result);
- System.out.println("请求返回信息: " + resultObj.toString());
- } catch (Exception e) {
- e.printStackTrace();
- String msg = MessageFormat.format("请求签名鉴权方式调用接口出现异常: {0}", e.getMessage());
- System.out.println(msg);
- }
- }
- /**
- * 查询企业用户授权详情
- */
- public static void getOrgUserInfo(String appId, String appKey, String host,String orgId) {
- // 计算签名拼接的url
- String getUrl = "/v3/organizations/"+orgId+"/authorized-info";
- // 完整的请求地址
- String getAllUrl = host + getUrl;
- try {
- // GET请求时ContentMD5为""
- String contentMD5 = "";
- // 构建待签名字符串
- String method = "GET";
- String accept = "*/*";
- String contentType = "application/json; charset=UTF-8";
- String url = getUrl;
- String date = "";
- String headers = "";
- StringBuffer sb = new StringBuffer();
- sb.append(method).append("\n").append(accept).append("\n").append(contentMD5).append("\n")
- .append(contentType).append("\n").append(date).append("\n");
- if ("".equals(headers)) {
- sb.append(headers).append(url);
- } else {
- sb.append(headers).append("\n").append(url);
- }
- // 构建参与请求签名计算的明文
- String plaintext = sb.toString();
- // 计算请求签名值
- String reqSignature = doSignatureBase64(plaintext, appKey);
- System.out.println("计算请求签名值:"+ reqSignature);
- // 获取时间戳(精确到毫秒)
- long timeStamp = timeStamp();
- // 构建请求头
- LinkedHashMap<String, String> header = new LinkedHashMap<String, String>();
- header.put("X-Tsign-Open-App-Id", appId);
- header.put("X-Tsign-Open-Auth-Mode", "Signature");
- header.put("X-Tsign-Open-Ca-Signature", reqSignature);
- header.put("X-Tsign-Open-Ca-Timestamp", String.valueOf(timeStamp));
- header.put("Accept", accept);
- header.put("Content-Type", contentType);
- header.put("Content-MD5", contentMD5);
- HashMap<String, Object> query = new HashMap<String, Object>();
- // query.put("orgName", orgName);
- // query.put("orgIDCardType", "CRED_ORG_USCC");
- // 发送GET请求
- String result = HTTPHelper.sendGet(getAllUrl, query, header, "UTF-8");
- JSONObject resultObj = JSONObject.parseObject(result);
- System.out.println("请求返回信息: " + resultObj.toString());
- } catch (Exception e) {
- e.printStackTrace();
- String msg = MessageFormat.format("请求签名鉴权方式调用接口出现异常: {0}", e.getMessage());
- System.out.println(msg);
- }
- }
- /***
- * 获取编辑合同模板页面
- */
- public static void getDocTemplateEditUrl(String appId, String appKey, String host,String docTemplateId) {
- // 计算签名拼接的url
- String postUrl = "/v3/doc-templates/" + docTemplateId + "/doc-template-edit-url";
- // 完整的请求地址
- String postAllUrl = host + postUrl;
- try {
- // 构建请求Body体
- JSONObject reqBodyObj = new JSONObject();
- // 请求Body体数据
- String reqBodyData = reqBodyObj.toString();
- // 对请求Body体内的数据计算ContentMD5
- String contentMD5 = doContentMD5(reqBodyData);
- System.out.println("请求body数据:"+reqBodyData);
- System.out.println("body的md5值:"+ contentMD5);
- // 构建待签名字符串
- String method = "POST";
- String accept = "*/*";
- String contentType = "application/json";
- String url = postUrl;
- String date = "";
- String headers = "";
- StringBuffer sb = new StringBuffer();
- sb.append(method).append("\n").append(accept).append("\n").append(contentMD5).append("\n")
- .append(contentType).append("\n").append(date).append("\n");
- if ("".equals(headers)) {
- sb.append(headers).append(url);
- } else {
- sb.append(headers).append("\n").append(url);
- }
- // 构建参与请求签名计算的明文
- String plaintext = sb.toString();
- // 计算请求签名值
- String reqSignature = doSignatureBase64(plaintext, appKey);
- System.out.println("计算请求签名值:"+reqSignature);
- // 获取时间戳(精确到毫秒)
- long timeStamp = timeStamp();
- // 构建请求头
- LinkedHashMap<String, String> header = new LinkedHashMap<String, String>();
- header.put("X-Tsign-Open-App-Id", appId);
- header.put("X-Tsign-Open-Auth-Mode", "Signature");
- header.put("X-Tsign-Open-Ca-Timestamp", String.valueOf(timeStamp));
- header.put("Accept", accept);
- header.put("Content-Type", contentType);
- header.put("X-Tsign-Open-Ca-Signature", reqSignature);
- header.put("Content-MD5", contentMD5);
- // 发送POST请求
- String result = HTTPHelper.sendPOST(postAllUrl, reqBodyData, header, "UTF-8");
- JSONObject resultObj = JSONObject.parseObject(result);
- System.out.println("请求返回信息: " + resultObj.toString());
- } catch (Exception e) {
- e.printStackTrace();
- String msg = MessageFormat.format("请求签名鉴权方式调用接口出现异常: {0}", e.getMessage());
- System.out.println(msg);
- }
- }
- /***
- * 查询签署流程详情
- */
- public static void getSignFlowDetail(String appId, String appKey, String host,String signFlowId) {
- // 计算签名拼接的url
- String getUrl = "/v3/sign-flow/"+signFlowId+"/detail";
- // 完整的请求地址
- String getAllUrl = host + getUrl;
- try {
- // GET请求时ContentMD5为""
- String contentMD5 = "";
- // 构建待签名字符串
- String method = "GET";
- String accept = "*/*";
- String contentType = "application/json; charset=UTF-8";
- String url = getUrl;
- String date = "";
- String headers = "";
- StringBuffer sb = new StringBuffer();
- sb.append(method).append("\n").append(accept).append("\n").append(contentMD5).append("\n")
- .append(contentType).append("\n").append(date).append("\n");
- if ("".equals(headers)) {
- sb.append(headers).append(url);
- } else {
- sb.append(headers).append("\n").append(url);
- }
- // 构建参与请求签名计算的明文
- String plaintext = sb.toString();
- // 计算请求签名值
- String reqSignature = doSignatureBase64(plaintext, appKey);
- System.out.println("计算请求签名值:"+ reqSignature);
- // 获取时间戳(精确到毫秒)
- long timeStamp = timeStamp();
- // 构建请求头
- LinkedHashMap<String, String> header = new LinkedHashMap<String, String>();
- header.put("X-Tsign-Open-App-Id", appId);
- header.put("X-Tsign-Open-Auth-Mode", "Signature");
- header.put("X-Tsign-Open-Ca-Signature", reqSignature);
- header.put("X-Tsign-Open-Ca-Timestamp", String.valueOf(timeStamp));
- header.put("Accept", accept);
- header.put("Content-Type", contentType);
- header.put("Content-MD5", contentMD5);
- HashMap<String, Object> query = new HashMap<String, Object>();
- // query.put("orgIDCardNum", "9100*****0004");
- // query.put("orgIDCardType", "CRED_ORG_USCC");
- // 发送GET请求
- String result = HTTPHelper.sendGet(getAllUrl, query, header, "UTF-8");
- JSONObject resultObj = JSONObject.parseObject(result);
- System.out.println("请求返回信息: " + resultObj.toString());
- } catch (Exception e) {
- e.printStackTrace();
- String msg = MessageFormat.format("请求签名鉴权方式调用接口出现异常: {0}", e.getMessage());
- System.out.println(msg);
- }
- }
- /***
- * 下载已签署文件及附属材料
- */
- public static void downloadFile(String appId, String appKey, String host,String signFlowId) {
- // 计算签名拼接的url
- String postUrl = "/v3/sign-flow/" + signFlowId + "/file-download-url";
- // 完整的请求地址
- String postAllUrl = host + postUrl;
- try {
- // 构建请求Body体
- JSONObject reqBodyObj = new JSONObject();
- // 请求Body体数据
- String reqBodyData = reqBodyObj.toString();
- // 对请求Body体内的数据计算ContentMD5
- String contentMD5 = doContentMD5(reqBodyData);
- System.out.println("请求body数据:"+reqBodyData);
- System.out.println("body的md5值:"+ contentMD5);
- // 构建待签名字符串
- String method = "POST";
- String accept = "*/*";
- String contentType = "application/json";
- String url = postUrl;
- String date = "";
- String headers = "";
- StringBuffer sb = new StringBuffer();
- sb.append(method).append("\n").append(accept).append("\n").append(contentMD5).append("\n")
- .append(contentType).append("\n").append(date).append("\n");
- if ("".equals(headers)) {
- sb.append(headers).append(url);
- } else {
- sb.append(headers).append("\n").append(url);
- }
- // 构建参与请求签名计算的明文
- String plaintext = sb.toString();
- // 计算请求签名值
- String reqSignature = doSignatureBase64(plaintext, appKey);
- System.out.println("计算请求签名值:"+reqSignature);
- // 获取时间戳(精确到毫秒)
- long timeStamp = timeStamp();
- // 构建请求头
- LinkedHashMap<String, String> header = new LinkedHashMap<String, String>();
- header.put("X-Tsign-Open-App-Id", appId);
- header.put("X-Tsign-Open-Auth-Mode", "Signature");
- header.put("X-Tsign-Open-Ca-Timestamp", String.valueOf(timeStamp));
- header.put("Accept", accept);
- header.put("Content-Type", contentType);
- header.put("X-Tsign-Open-Ca-Signature", reqSignature);
- header.put("Content-MD5", contentMD5);
- // 发送POST请求
- String result = HTTPHelper.sendPOST(postAllUrl, reqBodyData, header, "UTF-8");
- JSONObject resultObj = JSONObject.parseObject(result);
- System.out.println("请求返回信息: " + resultObj.toString());
- } catch (Exception e) {
- e.printStackTrace();
- String msg = MessageFormat.format("请求签名鉴权方式调用接口出现异常: {0}", e.getMessage());
- System.out.println(msg);
- }
- }
- /***
- * 获取签署页面链接
- */
- public static void getSignUrl(String appId, String appKey, String host,String signFlowId,String psnId) {
- // 计算签名拼接的url
- String postUrl = "/v3/sign-flow/"+signFlowId+"/sign-url";
- // 完整的请求地址
- String postAllUrl = host + postUrl;
- try {
- // 构建请求Body体
- JSONObject reqBodyObj = new JSONObject();
- Map operator = new HashMap();
- operator.put("psnId", psnId);
- reqBodyObj.put("operator", operator);
- // 请求Body体数据
- String reqBodyData = reqBodyObj.toString();
- // 对请求Body体内的数据计算ContentMD5
- String contentMD5 = doContentMD5(reqBodyData);
- System.out.println("请求body数据:"+reqBodyData);
- System.out.println("body的md5值:"+ contentMD5);
- // 构建待签名字符串
- String method = "POST";
- String accept = "*/*";
- String contentType = "application/json";
- String url = postUrl;
- String date = "";
- String headers = "";
- StringBuffer sb = new StringBuffer();
- sb.append(method).append("\n").append(accept).append("\n").append(contentMD5).append("\n")
- .append(contentType).append("\n").append(date).append("\n");
- if ("".equals(headers)) {
- sb.append(headers).append(url);
- } else {
- sb.append(headers).append("\n").append(url);
- }
- // 构建参与请求签名计算的明文
- String plaintext = sb.toString();
- // 计算请求签名值
- String reqSignature = doSignatureBase64(plaintext, appKey);
- System.out.println("计算请求签名值:"+reqSignature);
- // 获取时间戳(精确到毫秒)
- long timeStamp = timeStamp();
- // 构建请求头
- LinkedHashMap<String, String> header = new LinkedHashMap<String, String>();
- header.put("X-Tsign-Open-App-Id", appId);
- header.put("X-Tsign-Open-Auth-Mode", "Signature");
- header.put("X-Tsign-Open-Ca-Timestamp", String.valueOf(timeStamp));
- header.put("Accept", accept);
- header.put("Content-Type", contentType);
- header.put("X-Tsign-Open-Ca-Signature", reqSignature);
- header.put("Content-MD5", contentMD5);
- // 发送POST请求
- String result = HTTPHelper.sendPOST(postAllUrl, reqBodyData, header, "UTF-8");
- JSONObject resultObj = JSONObject.parseObject(result);
- System.out.println("请求返回信息: " + resultObj.toString());
- } catch (Exception e) {
- e.printStackTrace();
- String msg = MessageFormat.format("请求签名鉴权方式调用接口出现异常: {0}", e.getMessage());
- System.out.println(msg);
- }
- }
- /***
- * 请求签名鉴权-POST请求
- *
- * @param appId=应用ID
- * @param appKey=应用密钥(AppSecret)
- * @param host
- */
- public static void testPost(String appId, String appKey, String host) {
- // 计算签名拼接的url
- String postUrl = "/v3/files/file-upload-url";
- // 完整的请求地址
- String postAllUrl = host + postUrl;
- try {
- // 构建请求Body体
- JSONObject reqBodyObj = new JSONObject();
- reqBodyObj.put("contentMd5", "KMYh+0qU9/FDXf2TwCGbeg==");
- reqBodyObj.put("contentType", "application/octet-stream");
- reqBodyObj.put("convertToPDF", "true");
- reqBodyObj.put("fileName", "销售合同.docx");
- reqBodyObj.put("fileSize", "81825");
- //reqBodyObj.put("convertToHTML", "false");
- // 请求Body体数据
- String reqBodyData = reqBodyObj.toString();
- // 对请求Body体内的数据计算ContentMD5
- String contentMD5 = doContentMD5(reqBodyData);
- System.out.println("请求body数据:"+reqBodyData);
- System.out.println("body的md5值:"+ contentMD5);
- // 构建待签名字符串
- String method = "POST";
- String accept = "*/*";
- String contentType = "application/json";
- String url = postUrl;
- String date = "";
- String headers = "";
- StringBuffer sb = new StringBuffer();
- sb.append(method).append("\n").append(accept).append("\n").append(contentMD5).append("\n")
- .append(contentType).append("\n").append(date).append("\n");
- if ("".equals(headers)) {
- sb.append(headers).append(url);
- } else {
- sb.append(headers).append("\n").append(url);
- }
- // 构建参与请求签名计算的明文
- String plaintext = sb.toString();
- // 计算请求签名值
- String reqSignature = doSignatureBase64(plaintext, appKey);
- System.out.println("计算请求签名值:"+reqSignature);
- // 获取时间戳(精确到毫秒)
- long timeStamp = timeStamp();
- // 构建请求头
- LinkedHashMap<String, String> header = new LinkedHashMap<String, String>();
- header.put("X-Tsign-Open-App-Id", appId);
- header.put("X-Tsign-Open-Auth-Mode", "Signature");
- header.put("X-Tsign-Open-Ca-Timestamp", String.valueOf(timeStamp));
- header.put("Accept", accept);
- header.put("Content-Type", contentType);
- header.put("X-Tsign-Open-Ca-Signature", reqSignature);
- header.put("Content-MD5", contentMD5);
- // 发送POST请求
- String result = HTTPHelper.sendPOST(postAllUrl, reqBodyData, header, "UTF-8");
- JSONObject resultObj = JSONObject.parseObject(result);
- System.out.println("请求返回信息: " + resultObj.toString());
- } catch (Exception e) {
- e.printStackTrace();
- String msg = MessageFormat.format("请求签名鉴权方式调用接口出现异常: {0}", e.getMessage());
- System.out.println(msg);
- }
- }
- /***
- * 查询企业成员列表
- */
- public static void getOrgMember(String appId, String appKey, String host,String orgId) {
- // 计算签名拼接的url
- String getUrl = "/v3/organizations/"+orgId+"/member-list?pageNum=1&pageSize=100";
- // 完整的请求地址
- String getAllUrl = host + getUrl;
- try {
- // GET请求时ContentMD5为""
- String contentMD5 = "";
- // 构建待签名字符串
- String method = "GET";
- String accept = "*/*";
- String contentType = "application/json; charset=UTF-8";
- String url = getUrl;
- String date = "";
- String headers = "";
- StringBuffer sb = new StringBuffer();
- sb.append(method).append("\n").append(accept).append("\n").append(contentMD5).append("\n")
- .append(contentType).append("\n").append(date).append("\n");
- if ("".equals(headers)) {
- sb.append(headers).append(url);
- } else {
- sb.append(headers).append("\n").append(url);
- }
- // 构建参与请求签名计算的明文
- String plaintext = sb.toString();
- // 计算请求签名值
- String reqSignature = doSignatureBase64(plaintext, appKey);
- System.out.println("计算请求签名值:"+ reqSignature);
- // 获取时间戳(精确到毫秒)
- long timeStamp = timeStamp();
- // 构建请求头
- LinkedHashMap<String, String> header = new LinkedHashMap<String, String>();
- header.put("X-Tsign-Open-App-Id", appId);
- header.put("X-Tsign-Open-Auth-Mode", "Signature");
- header.put("X-Tsign-Open-Ca-Signature", reqSignature);
- header.put("X-Tsign-Open-Ca-Timestamp", String.valueOf(timeStamp));
- header.put("Accept", accept);
- header.put("Content-Type", contentType);
- header.put("Content-MD5", contentMD5);
- HashMap<String, Object> query = new HashMap<String, Object>();
- // query.put("orgIDCardNum", "9100*****0004");
- // query.put("orgIDCardType", "CRED_ORG_USCC");
- // 发送GET请求
- String result = HTTPHelper.sendGet(getAllUrl, query, header, "UTF-8");
- JSONObject resultObj = JSONObject.parseObject(result);
- System.out.println("请求返回信息: " + resultObj.toString());
- } catch (Exception e) {
- e.printStackTrace();
- String msg = MessageFormat.format("请求签名鉴权方式调用接口出现异常: {0}", e.getMessage());
- System.out.println(msg);
- }
- }
- /***
- * 请求签名鉴权-GET请求
- *
- * @param appId=应用ID
- * @param appKey=应用密钥(AppSecret)
- * @param host
- */
- public static void testGet(String appId, String appKey, String host,String signFlowId) {
- // 计算签名拼接的url
- String getUrl = "/v3/sign-flow/"+signFlowId+"/detail";
- // 完整的请求地址
- String getAllUrl = host + getUrl;
- try {
- // GET请求时ContentMD5为""
- String contentMD5 = "";
- // 构建待签名字符串
- String method = "GET";
- String accept = "*/*";
- String contentType = "application/json; charset=UTF-8";
- String url = getUrl;
- String date = "";
- String headers = "";
- StringBuffer sb = new StringBuffer();
- sb.append(method).append("\n").append(accept).append("\n").append(contentMD5).append("\n")
- .append(contentType).append("\n").append(date).append("\n");
- if ("".equals(headers)) {
- sb.append(headers).append(url);
- } else {
- sb.append(headers).append("\n").append(url);
- }
- // 构建参与请求签名计算的明文
- String plaintext = sb.toString();
- // 计算请求签名值
- String reqSignature = doSignatureBase64(plaintext, appKey);
- System.out.println("计算请求签名值:"+ reqSignature);
- // 获取时间戳(精确到毫秒)
- long timeStamp = timeStamp();
- // 构建请求头
- LinkedHashMap<String, String> header = new LinkedHashMap<String, String>();
- header.put("X-Tsign-Open-App-Id", appId);
- header.put("X-Tsign-Open-Auth-Mode", "Signature");
- header.put("X-Tsign-Open-Ca-Signature", reqSignature);
- header.put("X-Tsign-Open-Ca-Timestamp", String.valueOf(timeStamp));
- header.put("Accept", accept);
- header.put("Content-Type", contentType);
- header.put("Content-MD5", contentMD5);
- HashMap<String, Object> query = new HashMap<String, Object>();
- // query.put("orgIDCardNum", "9100*****0004");
- // query.put("orgIDCardType", "CRED_ORG_USCC");
- // 发送GET请求
- String result = HTTPHelper.sendGet(getAllUrl, query, header, "UTF-8");
- JSONObject resultObj = JSONObject.parseObject(result);
- System.out.println("请求返回信息: " + resultObj.toString());
- } catch (Exception e) {
- e.printStackTrace();
- String msg = MessageFormat.format("请求签名鉴权方式调用接口出现异常: {0}", e.getMessage());
- System.out.println(msg);
- }
- }
- /***
- *
- * @param str 待计算的消息
- * @return MD5计算后摘要值的Base64编码(ContentMD5)
- * @throws Exception 加密过程中的异常信息
- */
- public static String doContentMD5(String str) throws Exception {
- byte[] md5Bytes = null;
- MessageDigest md5 = null;
- String contentMD5 = null;
- try {
- md5 = MessageDigest.getInstance("MD5");
- // 计算md5函数
- md5.update(str.getBytes("UTF-8"));
- // 获取文件MD5的二进制数组(128位)
- md5Bytes = md5.digest();
- // 把MD5摘要后的二进制数组md5Bytes使用Base64进行编码(而不是对32位的16进制字符串进行编码)
- contentMD5 = new String(Base64.encodeBase64(md5Bytes), "UTF-8");
- } catch (NoSuchAlgorithmException e) {
- String msg = MessageFormat.format("不支持此算法: {0}", e.getMessage());
- Exception ex = new Exception(msg);
- ex.initCause(e);
- throw ex;
- } catch (UnsupportedEncodingException e) {
- String msg = MessageFormat.format("不支持的字符编码: {0}", e.getMessage());
- Exception ex = new Exception(msg);
- ex.initCause(e);
- throw ex;
- }
- return contentMD5;
- }
- /***
- * 计算请求签名值
- *
- * @param message 待计算的消息
- * @param secret 密钥
- * @return HmacSHA256计算后摘要值的Base64编码
- * @throws Exception 加密过程中的异常信息
- */
- public static String doSignatureBase64(String message, String secret) throws Exception {
- String algorithm = "HmacSHA256";
- Mac hmacSha256;
- String digestBase64 = null;
- try {
- hmacSha256 = Mac.getInstance(algorithm);
- byte[] keyBytes = secret.getBytes("UTF-8");
- byte[] messageBytes = message.getBytes("UTF-8");
- hmacSha256.init(new SecretKeySpec(keyBytes, 0, keyBytes.length, algorithm));
- // 使用HmacSHA256对二进制数据消息Bytes计算摘要
- byte[] digestBytes = hmacSha256.doFinal(messageBytes);
- // 把摘要后的结果digestBytes使用Base64进行编码
- digestBase64 = new String(Base64.encodeBase64(digestBytes), "UTF-8");
- } catch (NoSuchAlgorithmException e) {
- String msg = MessageFormat.format("不支持此算法: {0}", e.getMessage());
- Exception ex = new Exception(msg);
- ex.initCause(e);
- throw ex;
- } catch (UnsupportedEncodingException e) {
- String msg = MessageFormat.format("不支持的字符编码: {0}", e.getMessage());
- Exception ex = new Exception(msg);
- ex.initCause(e);
- throw ex;
- } catch (InvalidKeyException e) {
- String msg = MessageFormat.format("无效的密钥规范: {0}", e.getMessage());
- Exception ex = new Exception(msg);
- ex.initCause(e);
- throw ex;
- }
- return digestBase64;
- }
- /***
- * 获取时间戳(毫秒级)
- *
- * @return 毫秒级时间戳,如 1578446909000
- */
- public static long timeStamp() {
- long timeStamp = System.currentTimeMillis();
- return timeStamp;
- }
- private static Map getRowMap(boolean insertRow,Object... columnValues) {
- Map row = new HashMap();
- for (int i = 0; i < columnValues.length; i++) {
- row.put("column" + (i + 1), columnValues[i]);
- }
- Map result = new HashMap();
- result.put("row",row);
- result.put("insertRow",insertRow);
- return result;
- }
- }
|