本文实例为大家分享了微信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 = TUtils.getAccessToken(APPID,API_KEY);
if (access_tokenStr != null
&& access_tokenStr.indexOf("access_token") != -1) {
try {
JSONObject jsonObject = new JSONObject(access_tokenStr);
access_token = jsonObject.getString("access_token");
a_expires_in = jsonObject.getString("expires_in");
atime = getCurrentDateStr();
} catch (JSONException e) {
// e.printStackTrace();
}
}
if (access_token != null && !access_token.equals("")) {
String ticketStr = TicketUtils.getJSAPITicket(access_token);
// System.out.println("ticketStr:" + ticketStr);
if (ticketStr != null && ticketStr.indexOf("ticket") != -1) {
try {
JSONObject jsonObject = new JSONObject(ticketStr);
ticket = jsonObject.getString("ticket");
t_expires_in = jsonObject.getString("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;
}
<div class="jb51code">
<pre class="brush:java;">
public static String getAccessToken(String APPID,String APPSECRET) {
String url = "https://api.weixin.qq.com/cgi-bin/token";
String params = "grant_type=client_credential&appid=" + APPID
- "&secret=" + APPSECRET;
String resultStr = HttpRequest.sendGet(url,params);
// sendGet:用get方法获取数据,具体请参考之间的关于微信的文章 http://www.cnblogs.com/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 = "https://api.weixin.qq.com/cgi-bin/ticket/getticket";
String params = "type=jsapi&access_token=" + access_token;
String resultStr = HttpRequest.sendGet(url,params);
return resultStr;
}