微信公众平台测试号注册
第一步是配置appID和appsecret,这两个值在个人设置可以看到,是微信给每一位用户提供的属性值,复制即可
第二步配置接口,url是项目中调用微信接口的url,需要是外网地址,可通过ngrok实现外网映射的功能
同时在项目中编写对应的Control层方法:
public static final String TOKEN = "yfkj_xfcamp_token";
@RequestMapping("get")
public void getToken(String signature,String timestamp,String nonce,String echostr,HttpServletResponse response) throws NoSuchAlgorithmException, IOException{
// 将token、timestamp、nonce三个参数进行字典序排序
System.out.println("signature:"+signature);
System.out.println("timestamp:"+timestamp);
System.out.println("nonce:"+nonce);
System.out.println("echostr:"+echostr);
System.out.println("TOKEN:"+TOKEN);
String[] params = new String[] { TOKEN, timestamp, nonce };
Arrays.sort(params);
// 将三个参数字符串拼接成一个字符串进行sha1加密
String clearText = params[0] + params[1] + params[2];
String algorithm = "SHA-1";
String sign = new String(
org.apache.commons.codec.binary.Hex.encodeHex(MessageDigest.getInstance(algorithm).digest((clearText).getBytes()), true));
// 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信
if (signature.equals(sign)) {
response.getWriter().print(echostr);
}
}
第三步配置域名,域名即外网域名
第四步,关注测试号,需要关注测试号才可以
最后,往下翻找到网页授权获取用户基本信息
点击修改,输入外网域名,搞定
到此,测试号已成功生成。
编写测试代码,获取用户信息
@GetMapping("login")
public void login(HttpServletResponse resp) throws IOException {
// 公众号:
String url = "https://open.weixin.qq.com/connect/oauth2/authorize?" + "appid=" + appkey.getAppID() + "&"
+ "redirect_uri=" + appkey.getRedirectUri() + "&" + "response_type=code&" + "scope=snsapi_userinfo&" // snsapi_base
+ "state=STATE#wechat_redirect";
/*
* String url = "https://open.weixin.qq.com/connect/qrconnect?" +
* "appid="+appkey.getAppID()+"&" + "redirect_uri="+appkey.getRedirectUri()+"&"
* + "response_type=code&" + "scope=snsapi_base&" +
* "state=STATE#wechat_redirect";
*/
resp.sendRedirect(url);
}
@GetMapping("get/callback")
public void callback(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String code = req.getParameter("code");
ObjectMapper objectMapper = new ObjectMapper();
String url = "https://api.weixin.qq.com/sns/oauth2/access_token?" + "appid=" + appkey.getAppID() + "&"
+ "secret=" + appkey.getAppSecret() + "&" + "code=" + code + "&" + "grant_type=authorization_code";
ResponseEntity<String> forEntity = restTemplate.getForEntity(url, String.class);
String str = null;
if (forEntity.getStatusCodeValue() == 200) {
str = forEntity.getBody();
}
AccessTokenBO accessTokenBO = null;
try {
accessTokenBO = objectMapper.readValue(str, AccessTokenBO.class);
} catch (IOException e) {
throw new RuntimeException(e.getMessage());
}
System.out.println(accessTokenBO.toString());
url = "https://api.weixin.qq.com/sns/userinfo?" + "access_token=" + accessTokenBO.getAccess_token() + "&"
+ "openid=" + accessTokenBO.getOpenid() + "&" + "lang=zh_CN";
forEntity = restTemplate.getForEntity(url, String.class);
if (forEntity.getStatusCodeValue() == 200) {
str = forEntity.getBody();
}
UserInfoBO userInfoBO = null;
try {
userInfoBO = objectMapper.readValue(str, UserInfoBO.class);
} catch (IOException e) {
throw new RuntimeException(e.getMessage());
}
System.out.println(userInfoBO.toString());
resp.sendRedirect("/index.html");
}
浙公网安备 33010602011771号