欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

微信JSAPI Ticket接口签名详解

程序员文章站 2023-02-25 13:51:55
本文实例为大家分享了微信js接口签名的具体代码,供大家参考,具体内容如下 1、微信 js 接口签名校验工具 2、具体开发 2.1 获取access_token,然...

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

1、微信 js 接口签名校验工具

微信JSAPI Ticket接口签名详解

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;
 }

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;
 }

2.2具体生成签名signature

public string wx_signature() {
  string path = servletactioncontext.getservletcontext().getrealpath(
    "/wx/");
  // system.out.println(path);
  try {
   string tokenjson = myfileutils.readtext(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 = jsonobject.getstring("access_token");//
     // access_token
     atime = jsonobject.getstring("atime");// 开始时间
     a_expires_in = jsonobject.getstring("a_expires_in");// 有效时间
     ticket = jsonobject.getstring("ticket");// jsapi_ticket
     // system.out.println(ticket);
     // ttime = jsonobject.getstring("ttime");// 开始时间
     // t_expires_in = jsonobject.getstring("t_expires_in");//
     // 有效时间
     string t1 = getcurrentdatestr();
     string t2 = atime;
     // system.out.println(atime);
     // system.out.println(a_expires_in);
     // system.out.println(timeinterval.getinterval(t2, t1));
     long end_time = long.parselong(a_expires_in) - 60;
     if (timeinterval.getinterval(t2, t1) > end_time) {
      ticket = getaccesstoken_ticket(path);
     }
    } catch (jsonexception e) {
     msg = e.getmessage();
    }
   } else {

   }
   // system.out.println(ticket);
   string url = getparameter("url");
   string noncestr = tutils.getrandomstring(16);
   string timestamp = system.currenttimemillis() + "";
   timestamp = timestamp.substring(0, 10);
   string data = "jsapi_ticket=" + ticket + "&noncestr=" + noncestr
     + "&timestamp=" + timestamp + "&url=" + url;
   string digest = new sha1().getdigestofstring(data.getbytes());
   string signature = digest.tolowercase();// signature
   result = "{\"noncestr\":\"" + noncestr + "\",\"timestamp\":\""
     + timestamp + "\",\"url\":\"" + url + "\",\"signature\":\""
     + signature + "\" ,\"ticket\":\"" + ticket + "\"}";
        msg = result;
  } catch (ioexception e) {
   msg = e.getmessage();
  }
  return msg 
 }

说明:签名是有调用次数,需要将其cache到服务器的文件中。

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