JsApiTicket获取

编程

一、jsapi_ticket是什么?


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

微信JS接口: 授权登录, 分享网页等

二、注意事项


由于jsapi_ticket有效期为2小时,并且每天有实时调用量上限次数,所以最好开发的时候最好把jsapi_ticket存储起来,判断上次获取的jsapi_ticket是否有效,如果有效,就需要再次发起请求去获取jsapi_ticket了。 

 

三, 获取

添加公众号白名单

 安全中心 > 白名单 (填写服务器IP地址)

java 代码

 

 

package org.xxx.xxx.wx.util;

import lombok.extern.slf4j.Slf4j;

import com.alibaba.fastjson.JSONObject;

import java.io.InputStream;

import java.net.HttpURLConnection;

import java.net.URL;

import java.security.MessageDigest;

import java.security.NoSuchAlgorithmException;

/**

* @Description:

* @Auther: wuxw

* @Date: 2019/11/20 17:38

*/

@Slf4j

public class WxSignUtil2 {

public static String getAccessToken() {

String access_token = "";

String grant_type = "client_credential";//获取access_token填写client_credential

String AppId="APPID";//第三方用户唯一凭证

String secret="秘钥";//第三方用户唯一凭证密钥,即appsecret

//这个url链接地址和参数皆不能变

String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type="+grant_type+"&appid="+AppId+"&secret="+secret;

try {

URL urlGet = new URL(url);

HttpURLConnection http = (HttpURLConnection) urlGet.openConnection();

http.setRequestMethod("GET"); // 必须是get方式请求

http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");

http.setDoOutput(true);

http.setDoInput(true);

System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 连接超时30秒

System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 读取超时30秒

http.connect();

InputStream is = http.getInputStream();

int size = is.available();

byte[] jsonBytes = new byte[size];

is.read(jsonBytes);

is.close();

String message = new String(jsonBytes, "UTF-8");

log.info("授权返回消息结构体: "+message);

JSONObject demoJson = JSONObject.parseObject(message);

System.out.println("授权返回消息结构体: JSON字符串:"+demoJson);

access_token = demoJson.getString("access_token");

} catch (Exception e) {

e.printStackTrace();

}

return access_token;

}

public static String getTicket(String access_token) {

String ticket = null;

String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="+ access_token +"&type=jsapi";//这个url链接和参数不能变

try {

URL urlGet = new URL(url);

HttpURLConnection http = (HttpURLConnection) urlGet.openConnection();

http.setRequestMethod("GET"); // 必须是get方式请求

http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");

http.setDoOutput(true);

http.setDoInput(true);

System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 连接超时30秒

System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 读取超时30秒

http.connect();

InputStream is = http.getInputStream();

int size = is.available();

byte[] jsonBytes = new byte[size];

is.read(jsonBytes);

String message = new String(jsonBytes, "UTF-8");

JSONObject demoJson = JSONObject.parseObject(message);

System.out.println("JSON字符串:"+demoJson);

ticket = demoJson.getString("ticket");

is.close();

} catch (Exception e) {

e.printStackTrace();

}

return ticket;

}

public static String SHA1(String decript) {

try {

MessageDigest digest = java.security.MessageDigest.getInstance("SHA-1");

digest.update(decript.getBytes());

byte messageDigest[] = digest.digest();

// Create Hex String

StringBuffer hexString = new StringBuffer();

// 字节数组转换为 十六进制 数

for (int i = 0; i < messageDigest.length; i++) {

String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);

if (shaHex.length() < 2) {

hexString.append(0);

}

hexString.append(shaHex);

}

return hexString.toString();

} catch (NoSuchAlgorithmException e) {

e.printStackTrace();

}

return "";

}

}

 

package org.linlinjava.litemall.wx.web;

import lombok.extern.slf4j.Slf4j;

import com.alibaba.fastjson.JSONObject;

import org.linlinjava.litemall.wx.util.WxSignUtil2;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import java.io.IOException;

import java.util.UUID;

/**

* @Description:

* @Auther: wuxw

* @Date: 2019/11/20 16:28

*/

@Slf4j

@Controller

@RequestMapping("/wx/sign/config")

public class WxSignConfigController {

@ResponseBody

@RequestMapping(value="/getJsapiTicket", method = RequestMethod.GET)

public Object t2(HttpServletRequest request, HttpServletResponse response) throws IOException {

log.info("-----------------------event2----------------------");

String url = request.getParameter("url");

//1、获取AccessToken

String accessToken = WxSignUtil2.getAccessToken();

//2、获取Ticket

String jsapi_ticket = WxSignUtil2.getTicket(accessToken);

//3、时间戳和随机字符串

String noncestr = UUID.randomUUID().toString().replace("-", "").substring(0, 16);//随机字符串

String timestamp = String.valueOf(System.currentTimeMillis() / 1000);//时间戳

System.out.println("accessToken:"+accessToken+"

jsapi_ticket:"+jsapi_ticket+"

时间戳:"+timestamp+"

随机字符串:"+noncestr);

//4、获取url

if(url == null){

url="www.xxx.vip/index.html";

}

/*根据JSSDK上面的规则进行计算,这里比较简单,我就手动写啦

String[] ArrTmp = {"jsapi_ticket","timestamp","nonce","url"};

Arrays.sort(ArrTmp);

StringBuffer sf = new StringBuffer();

for(int i=0;i<ArrTmp.length;i++){

sf.append(ArrTmp[i]);

}

*/

//5、将参数排序并拼接字符串

String str = "jsapi_ticket="+jsapi_ticket+"&noncestr="+noncestr+"&timestamp="+timestamp+"&url="+url;

//6、将字符串进行sha1加密

String signature =WxSignUtil2.SHA1(str);

System.out.println("参数:"+str+"

签名:"+signature);

JSONObject obj = new JSONObject();

obj.put("jsapi_ticket",jsapi_ticket);

obj.put("noncestr",noncestr);

obj.put("timestamp",timestamp);

obj.put("url",url);

return obj;

}

}

 

以上是 JsApiTicket获取 的全部内容, 来源链接: utcz.com/z/510989.html

回到顶部