前置需求:
在mp.weixin.qq.cpm申请自己的微信公众号
1.务必修改:
1.com.may.wx.util.SignatureUtil类的appid,否则无法调用api
2.com.may.wx.util.TokenUtil类的appid
3.com.may.wx.util.TokenUtil类的secret
appip和密钥来自于mp.weixin.qq.com中的开发者设置
密钥只会显示一次,请记住,否则以后需要重置
com.may.wx.util.TokenUtil类的secret :
private static void setToken() throws Exception {
String appid = "你的appid";
String secret = "你的密钥";
URL url = new URL("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appid+"&secret="+secret);
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuffer sb = new StringBuffer();
String text = null;
while((text = br.readLine())!=null) {
sb.append(text);
}
br.close();
JSONObject obj = new JSONObject(sb.toString());
token = obj.getString("access_token");
}
com.may.wx.util.SignatureUtil类:
// 获取config初始化参数
public static Config getConfig(String url) throws Exception {
Config config = new Config();
//TODO 这里一定要设置, 否则无法使用微信SDK
config.setAppid("开发者ID");
String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
config.setTimestamp(timestamp);
String nonceStr = String.valueOf(ThreadLocalRandom.current().nextInt(89999999) + 10000000);
config.setNonceStr(nonceStr);
String signature = getSignature(TicketUtil.getTicket(), nonceStr, timestamp, url);
config.setSignature(signature);
config.setUrl(url);
return config;
}
2.添加JS安全域名:
自己用ngrok官网定义的xxx.itdage.com
将下载下来的txt文件找到项目,复制放在web文件夹中:
此时,可复制这个JS安全域名,加上
/MP_verify_TmpjGPqwLI0gY6VK.txt
可找到这个txt文件
注:只有把这个txt文件放到你的本地服务器中,并认证,微信才会识别你的服务器,而不是胡乱填写的
确保 先在服务器识别了这个txt文件以后,再提交你的JS接口安全域名!
3.添加白名单ip:
1.映射服务器地址:64.69.43.237(不一定是这个地址),可通过访问项目网页后,通过检查Headers得知。(只复制冒号之前的部分)
2.本机ip地址:百度或360搜索:ip查看本机ip地址
在以上的IP白名单中添加 映射服务器地址 和 本机IP地址 并保存