tofacebook.com -专业IT技术社区(指牛堂) java获取微信jsapi_ticket方法 - 指牛堂-专业IT技术平台
233°

java获取微信jsapi_ticket方法

首先要在公共账号中绑定域名,进入公共账号--设置


设置js安全域名


设置好以后就可以开始开发

可以参照https://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html#.E9.99.84.E5.BD.951-JS-SDK.E4.BD.BF.E7.94.A8.E6.9D.83.E9.99.90.E7.AD.BE.E5.90.8D.E7.AE.97.E6.B3.95


获取jsapi_ticket

jsapi_ticket是公众号用于调用微信JS接口的临时票据。正常情况下, jsapi_ticket的有效期为7200秒 ,通过access_token来获取。由于获取jsapi_ticket的api调用次数非常有限,频繁刷新jsapi_ticket会导致api调用受限,影响自身业务, 开发者必须在自己的服务全局缓存jsapi_ticket  


在数据库中加入一张表专门用来存放/刷新jsapi_ticket用的,里面有失效时间 每次更新时间和失效时间相差10分钟(有效时间1小时50分钟) 保证在没有失效前就去刷新

每次先去查看有没有未失效的jsapi_ticket  如果有就直接取到后去生成签名  
如果没有则去请求access_token 用access_token在去请求jsapi_ticket

上代码

  1. @Service
  2. public class JssdkService {
  3. private final String appid = "公共账号id";
  4. private final String secret = "公共账号secret";
  5. @Autowired
  6. private JssdkDao jssdkDao;
  7. //url为分享的url
  8. public Jssdk getInfo(String url) {
  9. Jssdk jssdk = jssdkDao.getInfo();
  10. String jsapi_ticket="";
  11. if(jssdk==null||StringUtils.isEmpty(jssdk.getJsapiTicket())){
  12. jsapi_ticket=getJsapiTicket();
  13. }else{
  14. jsapi_ticket=jssdk.getJsapiTicket();
  15. }
  16. jssdk=getSign(url,jsapi_ticket);
  17. return jssdk;
  18. }
  19. private String getJsapiTicket(){
  20. String requestUrl = "https://api.weixin.qq.com/cgi-bin/token?";
  21. String params = "grant_type=client_credential&appid=" + appid + "&secret=" + secret + "";
  22. String result = HttpRequestUtils.httpGet(requestUrl+params);
  23. String access_token = JSONObject.parseObject(result).getString("access_token");
  24. requestUrl = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?";
  25. params = "access_token=" + access_token + "&type=jsapi";
  26. result = HttpRequestUtils.httpGet(requestUrl+params);
  27. String jsapi_ticket = JSONObject.parseObject(result).getString("ticket");
  28. int activeTime=Integer.parseInt(JSONObject.parseObject(result).getString("expires_in"));
  29. Jssdk jssdk = new Jssdk();
  30. jssdk.setActiveTime(activeTime-600);
  31. jssdk.setJsapiTicket(jsapi_ticket);
  32. jssdkDao.update(jssdk);
  33. return jsapi_ticket;
  34. }
  35. private Jssdk getSign(String url,String jsapi_ticket) {
  36. String noncestr = getNonceStr();
  37. String timestamp = getTimeStamp();
  38. String sign = "jsapi_ticket=" + jsapi_ticket + "&noncestr=" + noncestr + "×tamp=" + timestamp + "&url=" + url;
  39. sign = Sha1Util.getSha1(sign);
  40. Jssdk jssdk = new Jssdk();
  41. jssdk.setAppid(appid);
  42. jssdk.setNoncestr(noncestr);
  43. jssdk.setTimestamp(timestamp);
  44. jssdk.setSign(sign);
  45. return jssdk;
  46. }
  47. private String getNonceStr() {
  48. Random random = new Random();
  49.         return MD5Util.getMessageDigest(String.valueOf(random.nextInt(10000)).getBytes());
  50. }
  51. private String getTimeStamp() {
  52. return String.valueOf(System.currentTimeMillis() / 1000);
  53. }


  1. import java.io.IOException;
  2. import java.net.URLDecoder;
  3. import org.apache.http.HttpResponse;
  4. import org.apache.http.client.methods.HttpGet;
  5. import org.apache.http.client.methods.HttpPost;
  6. import org.apache.http.impl.client.DefaultHttpClient;
  7. import org.apache.http.util.EntityUtils;
  8. import org.slf4j.Logger;
  9. import org.slf4j.LoggerFactory;
  10. public class HttpRequestUtils {
  11. private static Logger logger = LoggerFactory.getLogger(HttpRequestUtils.class); //日志记录
  12. /**
  13. * post请求
  14. * @param url url地址
  15. * @return
  16. */
  17. public static String httpPost(String url){
  18. //post请求返回结果
  19. DefaultHttpClient httpClient = new DefaultHttpClient();
  20. HttpPost method = new HttpPost(url);
  21. String str = "";
  22. try {
  23. HttpResponse result = httpClient.execute(method);
  24. url = URLDecoder.decode(url, "UTF-8");
  25. /**请求发送成功,并得到响应**/
  26. if (result.getStatusLine().getStatusCode() == 200) {
  27. try {
  28. /**读取服务器返回过来的json字符串数据**/
  29. str = EntityUtils.toString(result.getEntity());
  30. } catch (Exception e) {
  31. logger.error("post请求提交失败:" + url, e);
  32. }
  33. }
  34. } catch (IOException e) {
  35. logger.error("post请求提交失败:" + url, e);
  36. }
  37. return str;
  38. }
  39. /**
  40. * 发送get请求
  41. * @param url 路径
  42. * @return
  43. */
  44. public static String httpGet(String url){
  45. //get请求返回结果
  46. String strResult = null;
  47. try {
  48. DefaultHttpClient client = new DefaultHttpClient();
  49. //发送get请求
  50. HttpGet request = new HttpGet(url);
  51. HttpResponse response = client.execute(request);
  52. /**请求发送成功,并得到响应**/
  53. if (response.getStatusLine().getStatusCode() == org.apache.http.HttpStatus.SC_OK) {
  54. /**读取服务器返回过来的json字符串数据**/
  55. strResult = EntityUtils.toString(response.getEntity());
  56. } else {
  57. logger.error("get请求提交失败:" + url);
  58. }
  59. } catch (IOException e) {
  60. logger.error("get请求提交失败:" + url, e);
  61. }
  62. return strResult;
  63. }
  64. }

json解析可以用alibaba.fastjson也可以用别的工具

已有 0 条评论

    我有话说: