宝哥软件园

微信JSAPI票证接口签名详解

编辑:宝哥软件园 来源:互联网 时间:2021-09-04

本文实例为大家分享了微信射流研究…接口签名的具体代码,供大家参考,具体内容如下

1、微信射流研究…接口签名校验工具

2、具体开发

2.1 获取访问令牌,然后jsapi_ticket

/** * 获取访问令牌,然后jsapi _ ticket */private String getAccessToken _ ticket(字符串路径){ String access _ token=null//access _ token String atime=null;//获取时间字符串a _ expires _ in=null//有效时间字符串票证=空;//jsapi _ ticket String ttime=null;//得到时间字符串t _ expires _ in=null//有效时间(s)String access _ token str=tutils。getaccesstoken(APPID,API _ KEY);if (access_tokenStr!=null access _ token字符串。indexof(' access _ token ')!=-1) {尝试{ JSON对象JSON对象=新的JSON对象(access _ token str);access _ token=JSonobject。getString(' access _ token ');a _ expires _ in=JSonobject。getString(' expires _ in ');atime=getCurrentDateStr();} catch(JSonexception e){//e . print stack trace();} } if (access_token!=null!访问令牌。equals(" " { String ticket str=ticket utils。getjsapi ticket(access _ token);//系统。出去。println('票证字符串: '票证字符串);if (ticketStr!=null ticketStr.indexOf('ticket ')!=-1) {尝试{ JSON对象JSON对象=新JSON对象(票证字符串);ticket=JSonobject。GetString('票证');t _ expires _ in=JSonobject。getString(' expires _ in ');ttime=getCurrentDateStr();} catch(JSonexception e){//e . print stack trace();} } }字符串结果=null如果(票!=null!票。equals('){ result=' { ' access _ token ' : ' ' access _ token ' ', ' a _ expires _ in ' : ', ' atime ' : ', ' ticket ' : ', ' t _ expires _ in ' : ', ' t _ expires _ in ' : ', ' ttime ' : '(if . myfileutils . writeintotext(路径,结果)){ //System.out.println('写入文件成功');//System.out.println(结果);} else { System.out.println('写入微信签名文件失败');} }返回结果;}

公共静态字符串getAccessToken(字符串APPID,字符串APPSECRET){字符串URL=' https://API。微信。QQ。com/CGI-bin/token ';string params=' grant _ type=client _ credential APPID=' APPID ' secret=' APPSECRET;字符串结果str=Httprequest。发送获取(网址,参数);//发送获取:用得到方法获取数据,具体请参考之间的关于微信的文章http://www.cnblogs.com/jiduoduo/p/5749363.htmlreturn结果串;}/** * 根据访问令牌获取票证{ 'errcode':0,' errmsg':'ok ',' ticket ' : * ' bxldikrxvbtpdhsm 05 e5u 5 suoxnkd 8-41zo3m hkoyn 5 of kwitdggnr 2 fwj0m 9 E8 nyzwkvzvdvtaugdshfka ' *,' expires _ in ' :7200 } * * @ param access _ token * @ return */public static String getjsapticket(String access _ access字符串参数=' type=jsapi access _ token=' access _ token;字符串结果str=Httprequest。发送获取(网址,参数);返回结果字符串}2.2具体生成签名签名

公共字符串wx _ Signature(){ 0字符串路径=servletactioncontext。getservletcontext().getRealPath('/wx/');//System.out.println(路径);尝试{字符串标记JSON=myfileutils。readtext(路径);//String access _ token=null;//access _ token String atime=null;//获取时间字符串a _ expires _ in=null//有效时间字符串票证=空;//jsapi _ ticket//String ttime=null;//得到时间//String t _ expires _ in=null;//有效时间字符串结果=令牌字符串;if(结果==null | |结果。equals(" " { token JSON=getAccessToken _ ticket(路径);} //System.out.println(结果);if (tokenJSON!=null!令牌JSON。equals(')标记JSON。indexof(' access _ token ')!=-1) {尝试{ JSON对象JSON对象=新JSON对象(令牌JSON);//access _ token=JSonobject。getString(' access _ token ');////access _ token atime=JSON object。getstring(' atime ');//开始时间a _ expires _ in=JSonobject。getStrIng(' a _ expires _ in ');//有效时间ticket=JSonobject。GetString('票证');//jsapi _ ticket//system。出去。println(票证);//ttime=jsonobject。getstring(' ttime ');//开始时间//t _ expires _ in=JSonobject。getStrIng(' t _ expires _ in ');////有效时间字符串t1=getCurrentDateStr();字符串t2=atime//system。出去。println(atime);//系统。出去。println(a _ expires _ in);//系统。出去。println(时间间隔。getinterval(T2,t1));长结束时间=长。parse long(a _ expires _ in)-60;if (TimeInterval.getInterval(t2,t1)end _ time){ ticket=getAccessToken _ ticket(路径);} } catch(JSonexception e){ msg=e . getmessage();} } else { } //System.out.println(票证);字符串URL=GetParameter(' URL ');字符串非波峰=短裙。getrandomstring(16);字符串时间戳=系统。current millis()' ';时间戳=时间戳。子串(0,10);字符串数据=' jsapi _ ticket=' ticket ' non estr=' non estr '时间戳='时间戳' url=' url字符串摘要=新SHA1().getDigestOfString(数据。getbytes());字符串签名=摘要。tolowercase();//签名结果=' { '非纹章' : ' '非纹章' ', '时间戳' : ' '时间戳' ', ' url ' : ' ' url ' ', '签名' : ' '签名' ', '票证' : ' '票证' ' }msg=结果;} catch(IOexception e){ msg=e . Getmessay();}返回消息}说明:签名是有调用次数,需要将其躲藏到服务器的文件中。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

更多资讯
游戏推荐
更多+