测试号地址:http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login
开发前 建议用ngrok直接把本地开放给外网,我就是这样做的,可以本地调试,非常方便 并将端口改为80
微信会自动分配APPID,APPSERCRET给你
接口配置信息:
URL : 填写你自己写的验证接口 验证成功才能配置 例:
http://qingtest.tunnel.qydev.com/mydemo/WXApi/Get
Token: 自定义
下面是URL JAVA 实现代码:
/**
* URL是开发者用来接收微信消息和事件的接口URL。Token可由开发者可以任意填写,用作生成签名(该Token会和接口URL中包含的Token进行比对,从而验证安全性)
* 开发者提交信息后,微信服务器将发送GET请求到填写的服务器地址URL上,GET请求携带四个参数
*
* @param signature
* @param timestamp
* @param nonce
* @param echostr
* @return
*/
public String Get(String signature, String timestamp, String nonce, String echostr) {
//读取配置文件中指定的TOKEN 与测试号配置一致
String WXAppToken = Property.GetXMLValuesByKey("/config/wechat.properties", "WXAppToken");
if (WXAppToken == null || WXAppToken.length() <= 0) {
return "请先设置Token!";
}
* URL是开发者用来接收微信消息和事件的接口URL。Token可由开发者可以任意填写,用作生成签名(该Token会和接口URL中包含的Token进行比对,从而验证安全性)
* 开发者提交信息后,微信服务器将发送GET请求到填写的服务器地址URL上,GET请求携带四个参数
*
* @param signature
* @param timestamp
* @param nonce
* @param echostr
* @return
*/
public String Get(String signature, String timestamp, String nonce, String echostr) {
//读取配置文件中指定的TOKEN 与测试号配置一致
String WXAppToken = Property.GetXMLValuesByKey("/config/wechat.properties", "WXAppToken");
if (WXAppToken == null || WXAppToken.length() <= 0) {
return "请先设置Token!";
}
//该方法在下面
if (!BaseAPI.CheckSignature(WXAppToken, signature, timestamp, nonce)) {
if (echostr == null || echostr.isEmpty()) {
return "参数错误!";
}
}
return echostr;
}
if (!BaseAPI.CheckSignature(WXAppToken, signature, timestamp, nonce)) {
if (echostr == null || echostr.isEmpty()) {
return "参数错误!";
}
}
return echostr;
}
根据指定规矩生成验证字符串
http://qingtest.tunnel.qydev.com/mydemo/WXApi/Get
package com.star.wechat;
import java.util.ArrayList;
import java.util.Collections;
import org.apache.commons.codec.digest.DigestUtils;
public class BaseAPI {
/*
* 规则描述 1. 将token、timestamp、nonce三个参数进行字典序排序 2. 将三个参数字符串拼接成一个字符串进行sha1加密
* 3.开发者获得加密后的字符串可与signature对比,标识该请求来源于微信
*/
public static boolean CheckSignature(String wXAppToken, String signature, String timestamp, String nonce) {
ArrayList<String> list = new ArrayList<String>();
list.add(nonce);
list.add(timestamp);
list.add(wXAppToken);
Collections.sort(list);
@SuppressWarnings("deprecation")
String sha1signature = DigestUtils.shaHex(list.get(0) + list.get(1) + list.get(2));
return signature.equals(sha1signature);
}
}
package com.star.wechat;
import java.util.ArrayList;
import java.util.Collections;
import org.apache.commons.codec.digest.DigestUtils;
public class BaseAPI {
/*
* 规则描述 1. 将token、timestamp、nonce三个参数进行字典序排序 2. 将三个参数字符串拼接成一个字符串进行sha1加密
* 3.开发者获得加密后的字符串可与signature对比,标识该请求来源于微信
*/
public static boolean CheckSignature(String wXAppToken, String signature, String timestamp, String nonce) {
ArrayList<String> list = new ArrayList<String>();
list.add(nonce);
list.add(timestamp);
list.add(wXAppToken);
Collections.sort(list);
@SuppressWarnings("deprecation")
String sha1signature = DigestUtils.shaHex(list.get(0) + list.get(1) + list.get(2));
return signature.equals(sha1signature);
}
}
http://qingtest.tunnel.qydev.com/mydemo/WXApi/Get