获取微信公众号openid
Posted 绿萝配白啤_
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了获取微信公众号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"); }
以上是关于获取微信公众号openid的主要内容,如果未能解决你的问题,请参考以下文章