Java 微信公众号网页开发相关
用户关注公众号点击链接 后端根据code换取asstoken
需要公众号后台配置配置域名
https://open.weixin.qq.com/connect/oauth2/authorize?appid= APPID &redirect_uri= URL 打开的页面 &response_type=code&scope=snsapi_userinfo&state=2#wechat_redirect
后台需要get接口 获取code 进行操作
获取asstoken
public JSONObject getToken(String code) {
// 使用前端code获取手机号码 参数为json格式
String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="+appid+"&secret="+secret+"&code=" +
code+"&grant_type=authorization_code";
Map<String, String> paramMap = new HashMap<>();
ResponseEntity<String> response = restTemplate.getForEntity(url, String.class, paramMap);
System.out.println("response"+response);
log.info("response={}",response);
JSONObject object=JSONObject.parseObject(response.getBody());
return object;
}...
根据accessToken 换取用户头像和昵称
获取用户信息
public JSONObject getWxUserInfo(String openId,String accessToken) {
String url = "https://api.weixin.qq.com/sns/userinfo?access_token="+accessToken+"&openid="+openId+"&lang=zh_CN";
Map<String, String> paramMap = new HashMap<>();
restTemplate.getMessageConverters().set(1,new StringHttpMessageConverter(StandardCharsets.UTF_8));
ResponseEntity<String> response = restTemplate.getForEntity(url, String.class, paramMap);
System.out.println("response"+response);
log.info("response={}",response);
JSONObject object=JSONObject.parseObject(response.getBody());
return object;
}...
评论 (0)