全网小程序源码分享

微信网页端获取用户信息

首先,这里也是看的官方文档https://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html 。

1、网页获取code,这里scope为snsapi_userinfo用户授权可以获取用户信息,到自己回调页面获取code,然后调后台程序

<script type="text/javascript" src="js/jquery-1.10.1.min.js"></script>
<script type="text/javascript">
  $(function(){	
	window.location="https://open.weixin.qq.com/connect/oauth2/authorize?appid=自己appid&redirect_uri=回调地址&response_type=code&scope=snsapi_userinfo&state=1&connect_redirect=1#wechat_redirect";
  });	
</script>

2、通过code获取access_token,特别注意我们通过网页code获取的access_token和公众号获取的access_token不一样,这里有个博客介绍的比较不错http://www.cnblogs.com/wellsoho/p/5089409.html

    /**
     * 获取token 和openid
     * @throws Exception
     */
    public static Map<String,String> getTokenAndOpenid(String wxCode) throws Exception{

        String appid = ConfKit.get("AppId");
        String secret = ConfKit.get("AppSecret");

        String urlStr = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="+appid+"&secret="+secret+"&code="+wxCode+"&grant_type=authorization_code";
        URL url = new URL(urlStr);
        BufferedReader bufr = new BufferedReader(new InputStreamReader(new BufferedInputStream(url.openStream()), "utf-8"));
        String line;
        StringBuffer sb=new StringBuffer();
        while((line=bufr.readLine())!=null){
            sb.append(line);
        }
        bufr.close();
        JSONObject jsonObject = JSONObject.parseObject(sb.toString());
        if(jsonObject.get("errcode") != null){
            throw new Exception(jsonObject.getString("errmsg"));
        }
        String access_token = String.valueOf(jsonObject.get("access_token"));
        String openid = String.valueOf(jsonObject.get("openid"));
        Map<String,String> map = new HashMap<>();
        map.put("access_token",access_token);
        map.put("openid",openid);
        return map;
    }

3、通过access_token和openid获取用户信息

    /**
     * 获取用户信息
     * @throws Exception
     */
    public static Map<String,Object> getUserInfo(String access_token,String openid) throws Exception{

        String urlStr = "https://api.weixin.qq.com/sns/userinfo?access_token="+access_token+"&openid="+openid+"&lang=zh_CN";
        URL url = new URL(urlStr);
        BufferedReader bufr = new BufferedReader(new InputStreamReader(new BufferedInputStream(url.openStream()), "utf-8"));
        String line;
        StringBuffer sb=new StringBuffer();
        while((line=bufr.readLine())!=null){
            sb.append(line);
        }
        bufr.close();
        JSONObject jsonObject = JSONObject.parseObject(sb.toString());
        if(jsonObject.get("errcode") != null){
            throw new Exception(jsonObject.getString("errmsg"));
        }

        Map<String,Object> map = new HashMap<>();
        map.put("openid",String.valueOf(jsonObject.get("openid")));
        map.put("nickname",String.valueOf(jsonObject.get("nickname")));
        map.put("sex",String.valueOf(jsonObject.get("sex")));
        map.put("city",String.valueOf(jsonObject.get("city")));
        map.put("province",String.valueOf(jsonObject.get("province")));
        map.put("country",String.valueOf(jsonObject.get("country")));
        map.put("headimgurl",String.valueOf(jsonObject.get("headimgurl")));
        return map;
    }

4、ok,这样就获取到用户信息了!

posted @ 2016-12-30 23:39  沃弗码客  阅读(607)  评论(0编辑  收藏  举报
全网小程序源码分享