生成signature有一下几步:
1、通过 appid + appsecert 获取公众号的 access_token
2、根据1的access_token来获取jsapi_token
3、随便弄一个字符串(长度不太清楚,16位及以内应该都可以)作为nonceStr、
4、生成当前的时间戳(timestamp)
5、使用jssdk的网页url
6、将这几个参数按字典序排列,使用SHA-1生成signnature,在wx.config中配置即可
具体如下:
1.访问 https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET ,拿到返回值,其中有access-token
其中 grant_type不用改,将 appid和secret修改成自己的就可以了。
访问网络路径,那返回参数,我使用的工具类如下【自行修改一下】:
/**
1、通过 appid + appsecert 获取公众号的 access_token
2、根据1的access_token来获取jsapi_token
3、随便弄一个字符串(长度不太清楚,16位及以内应该都可以)作为nonceStr、
4、生成当前的时间戳(timestamp)
5、使用jssdk的网页url
6、将这几个参数按字典序排列,使用SHA-1生成signnature,在wx.config中配置即可
具体如下:
1.访问 https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET ,拿到返回值,其中有access-token
其中 grant_type不用改,将 appid和secret修改成自己的就可以了。
访问网络路径,那返回参数,我使用的工具类如下【自行修改一下】:
* 向指定url,发送get方式的请求
*/
public static String sendGet(String url, String param){
String result = "";
BufferedReader in = null;
try {
String urlNameString = url + "?" + param;
URL realUrl = new URL(urlNameString);
//打开和url之间的连接
URLConnection connection = realUrl.openConnection();
//设置通用参数
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
//建立实际连接
connection.connect();
//获取所有响应头字段
Map<String, List<String>> map = connection.getHeaderFields();
for(String key : map.keySet()){
System.out.println(key+"----"+map.get(key));
}
// 定义 BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while((line = in.readLine()) != null){
result += line;
}
} catch (Exception e) {
System.out.println("发送GET请求出现异常");
e.printStackTrace();
} finally{
try {
if(in != null){
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return result;
}
2.使用access-token获取ticket
-将acess_token替换即可,使用上面的方法获取返回值 ticket3.生成signature
public String
getSignature(String jsapiTicket,String rand,String timestamp,String url){
//字符串的变量顺序不能变,然后rand是随机字符串,timestamp=System.currentTimeMillis()/1000
//字符串的变量顺序不能变,然后rand是随机字符串,timestamp=System.currentTimeMillis()/1000
String string1 = "jsapi_ticket=" + jsapiTicket +
"&noncestr=" + rand +
"×tamp=" + timestamp +
"&url=" + url;
System.out.println(string1);
return encrypt(string1);
}
/**
* 加密
* @param strSrc
* @return
*/
private String encrypt(String strSrc) {
MessageDigest md = null;
String strDes = null;
byte[] bt = strSrc.getBytes();
try {
//////////////使用SHA-1的方式进行加密////////////
md = MessageDigest.getInstance("SHA-1"); //获取加密方式
md.update(bt);//加密
strDes = bytes2Hex(md.digest()); //返回16进制字符串
//////////////加密过程/////////
} catch (NoSuchAlgorithmException e) {
System.out.println("Invalid algorithm.");
return null;
}
return strDes;
}
/**
* 对加密后的字节数组进行转换
* @param bts
* @return
*/
public String bytes2Hex(byte[] bts) {
String des = "";
String tmp = null;
for (int i = 0; i < bts.length; i++) {
tmp = (Integer.toHexString(bts[i] & 0xFF));
if (tmp.length() == 1) {
des += "0";
}
des += tmp;
}
return des;
}
4.将对应的值填入即可
wx.config({
debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。 appId: '', // 必填,公众号的唯一标识
timestamp: , // 必填,生成签名的时间戳
nonceStr: '', // 必填,生成签名的随机串
signature: '',// 必填,签名,见附录1
jsApiList: [] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2});