微信网页端获取用户信息

首先,这里也是看的官方文档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,这样就获取到用户信息了!

转载于:https://www.cnblogs.com/hz-cww/p/6238545.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值