获取微信公众号openid

@Controller public class GetOpenId { private static String appid = ""; private static String appscript = ""; private static String redirect_uri = "...../getCode"; @RequestMapping("/getopenid") @ResponseBody public void getopenid( HttpServletResponse response) { String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid + "&redirect_uri=" + redirect_uri + "&response_type=code&scope=snsapi_userinfo&state=1234#wechat_redirect"; try { response.sendRedirect(url); //重定向跳转到url } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @RequestMapping("/getCode") @ResponseBody public void getCode(String code, String state, HttpServletResponse response) { String path = "https://api.weixin.qq.com/sns/oauth2/access_token?"; String params = "appid=" + appid + "&secret=" + appscript + "&code=" + code + "&grant_type=authorization_code"; String parm = Utils.sendPost(path, params); JSONObject json = JSONObject.fromObject(parm); String openid = json.getString("openid"); String accessToken = json.getString("access_token"); }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zzzjxz.html