@RequestMapping("/wxcode")
public void wxlogin(HttpServletResponse response) {
try {
// 第一步:用户同意授权,获取code
String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid +
"&redirect_uri=" + "http:ip+端口/wxcallback" +
"&response_type=code" +
"&scope=snsapi_userinfo" +
"&state=STATE#wechat_redirect";
response.sendRedirect(url);
} catch (IOException e) {
e.printStackTrace();
}
}
@RequestMapping("/wxcallback")
public String wxcallback(String code){
// 第二步:通过code换取网页授权access_token
String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appid +
"&secret=" + appsecret +
"&code=" + code +
"&grant_type=authorization_code";
ResponseEntity<String> res = restTemplate.getForEntity(url, String.class);
JSONObject jsonObject = JSONObject.parseObject(res.getBody());
System.out.println(jsonObject);
return jsonObject.toString();
}
还没有评论,来说两句吧...