码迷,mamicode.com
首页 > 微信 > 详细

获取微信公众号openid

时间:2016-11-15 13:54:52      阅读:392      评论:0      收藏:0      [点我收藏+]

标签:openid   pos   http   getc   pps   code   https   重定向   pen   


@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

标签:openid   pos   http   getc   pps   code   https   重定向   pen   

原文地址:http://www.cnblogs.com/webRongS/p/6064939.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!