According to WeChat’s official documents and case code, the above three parameters are necessary, and the above three parameters are obtained dynamically. Then, next, we use java code to achieve the acquisition according to WeChat’s official documents. The three parameters of timestamp, nonceStr, and signature are just a main method here to print and output. This lesson does not implement passing these three parameters to the web page and successfully calling up the WeChat jsapi. The next lesson will focus on the explanation. .
##Sign code:
The variable url in the main method on line 16 is the one you requested action or controller address, and the method jumps directly to the jsp interface using jsapi
Get the jsapi_ticket tool class code:
#package com.test.util; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import net.sf.json.JSONObject; import com.test.weixin.TestAcessToken; /*** * @author V型知识库 www.vxzsk.com * */ public class JsapiTicketUtil { /*** * 模拟get请求 * @param url * @param charset * @param timeout * @return */ public static String sendGet(String url, String charset, int timeout) { String result = ""; try { URL u = new URL(url); try { URLConnection conn = u.openConnection(); conn.connect(); conn.setConnectTimeout(timeout); BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), charset)); String line=""; while ((line = in.readLine()) != null) { result = result + line; } in.close(); } catch (IOException e) { return result; } } catch (MalformedURLException e) { return result; } return result; } public static String getAccessToken(){ String appid="你公众号基本设置里的应用id";//应用ID String appSecret="你公众号基本设置里的应用密钥";//(应用密钥) String url ="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appid+"&secret="+appSecret+""; String backData=TestAcessToken.sendGet(url, "utf-8", 10000); String accessToken = (String) JSONObject.fromObject(backData).get("access_token"); return accessToken; } public static String getJSApiTicket(){ //获取token String acess_token= JsapiTicketUtil.getAccessToken(); String urlStr = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="+acess_token+"&type=jsapi"; String backData=TestAcessToken.sendGet(urlStr, "utf-8", 10000); String ticket = (String) JSONObject.fromObject(backData).get("ticket"); return ticket; } public static void main(String[] args) { String jsapiTicket = JsapiTicketUtil.getJSApiTicket(); System.out.println("调用微信jsapi的凭证票为:"+jsapiTicket); } } Copy after login |
The above is the detailed content of Java for WeChat development to obtain WeChat timestamp, nonceStr, signature method. For more information, please follow other related articles on the PHP Chinese website!