JSAPITicket接⼝签名详解本⽂实例为⼤家分享了JS接⼝签名的具体代码,供⼤家参考,具体内容如下
1、 JS 接⼝签名校验⼯具
2、具体开发
2.1 获取access_token,然后jsapi_ticket
/**
* 获取access_token,然后jsapi_ticket
*/
private String getAccessToken_ticket(String path) {
String access_token = null; // access_token
String atime = null;// 获取时间
String a_expires_in = null;// 有效时间(s)
String ticket = null;// jsapi_ticket
String ttime = null;// 得到时间
String t_expires_in = null;// 有效时间(s)
String access_tokenStr = AccessToken(APPID,
API_KEY);
if (access_tokenStr != null
&& access_tokenStr.indexOf("access_token") != -1) {
try {
JSONObject jsonObject = new JSONObject(access_tokenStr);
access_token = String("access_token");
a_expires_in = String("expires_in");
atime = getCurrentDateStr();
} catch (JSONException e) {
/
/ e.printStackTrace();
}
}
if (access_token != null && !access_token.equals("")) {
String ticketStr = JSAPITicket(access_token);
// System.out.println("ticketStr:" + ticketStr);
if (ticketStr != null && ticketStr.indexOf("ticket") != -1) {
try {
JSONObject jsonObject = new JSONObject(ticketStr);
ticket = String("ticket");
t_expires_in = String("expires_in");
ttime = getCurrentDateStr();
} catch (JSONException e) {
// e.printStackTrace();
}
}
}
String result = null;
if (ticket != null && !ticket.equals("")) {
result = "{\"access_token\":\"" + access_token
+ "\",\"a_expires_in\":\"" + a_expires_in
+ "\",\"atime\":\"" + atime + "\",\"ticket\":\"" + ticket
+ "\",\"t_expires_in\":\"" + t_expires_in
+ "\",\"ttime\":\"" + ttime + "\"}";
if (MyFileUtils.writeIntoText(path, result)) {
// System.out.println("写⼊⽂件成功");
// System.out.println(result);
} else {
System.out.println("写⼊签名⽂件失败");
}
}
return result;
}
public static String getAccessToken(String APPID, String APPSECRET) {
String url = "api.weixin.qq/cgi-bin/token";
String params = "grant_type=client_credential&appid=" + APPID
+ "&secret=" + APPSECRET;
String resultStr = HttpRequest.sendGet(url, params);
// sendGet:⽤get⽅法获取数据 ,具体请参考之间的关于的⽂章 wwwblogs/jiduoduo/p/5749363.html return resultStr;
}
/**
* 根据access_token获取ticket { "errcode":0, "errmsg":"ok", "ticket":
* "bxLdikRXVbTPdHSM05e5u5sUoXNKd8-41ZO3MhKoyN5OfkWITDGgnr2fwJ0m9E8NYzWKVZvdVtaUgWvsdshFKA" * , "expires_in":7200 }
*
* @param access_token
* @return
*/
public static String getJSAPITicket(String access_token) {
String url = "api.weixin.qq/cgi-bin/ticket/getticket";
String params = "type=jsapi&access_token=" + access_token;
String resultStr = HttpRequest.sendGet(url, params);
return resultStr;
}
2.2具体⽣成签名signature
public String Wx_Signature() {
String path = ServletContext().getRealPath(
"/wx/");
// System.out.println(path);
try {
String tokenJSON = adText(path);
// String access_token = null; // access_token
String atime = null;// 获取时间
String a_expires_in = null;// 有效时间(s)
String ticket = null;// jsapi_ticket
// String ttime = null;// 得到时间
/
/ String t_expires_in = null;// 有效时间(s)
String result = tokenJSON;
if (result == null || result.equals("")) {
tokenJSON = getAccessToken_ticket(path);
}
// System.out.println(result);
if (tokenJSON != null && !tokenJSON.equals("")
&& tokenJSON.indexOf("access_token") != -1) {
try {
JSONObject jsonObject = new JSONObject(tokenJSON);
// access_token = String("access_token");//
/
/ access_token
atime = String("atime");// 开始时间
a_expires_in = String("a_expires_in");// 有效时间
ticket = String("ticket");// jsapi_ticket
// System.out.println(ticket);
// ttime = String("ttime");// 开始时间
// t_expires_in = String("t_expires_in");//
// 有效时间
String t1 = getCurrentDateStr();
String t2 = atime;
// System.out.println(atime);
/
/ System.out.println(a_expires_in);
// System.out.Interval(t2, t1));
long end_time = Long.parseLong(a_expires_in) - 60;
if (Interval(t2, t1) > end_time) {
ticket = getAccessToken_ticket(path);
}
} catch (JSONException e) {
msg = e.getMessage();
}
} else {
}api接口和sdk接口的区别
/
/ System.out.println(ticket);
String url = getParameter("url");
String noncestr = RandomString(16);
String timestamp = System.currentTimeMillis() + "";
timestamp = timestamp.substring(0, 10);
String data = "jsapi_ticket=" + ticket + "&noncestr=" + noncestr
+ "×tamp=" + timestamp + "&url=" + url;
String digest = new SHA1().Bytes());
String signature = LowerCase();// signature
result = "{\"noncestr\":\"" + noncestr + "\",\"timestamp\":\""
+ timestamp + "\",\"url\":\"" + url + "\",\"signature\":\""
+ signature + "\" ,\"ticket\":\"" + ticket + "\"}";
        msg = result;
} catch (IOException e) {
msg = e.getMessage();
}
return msg
}
说明:签名是有调⽤次数,需要将其cache到服务器的⽂件中。
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。