微信公众号网页授权获取用户信息

21 篇文章 1 订阅
5 篇文章 1 订阅

之前写了公众号项目,这几天需求网页直接获取用户信息小编直接用之前的获取openid接口的方式一顿操作······猛如虎,结果到官网一看网页授权的接口和之前用的不一样(定睛一看原地杵)~下面代码搞起来

首先前端页面生成二维码:

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8">
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=Edge, chrome=1">
        <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1">
        <title>XXX</title>
        <script src="/js/jquery-1.11.1.min.js"></script>
        <script src="/js/jquery.qrcode.js" type="text/javascript" charset="utf-8"></script>
    </head>
    <body>        
        <!--生成二维码位置-->
        <div class="weixin-tip" style="width:150px; float:left;"></div>
        <script language="javascript">
            $(function() {
                $(".weixin-tip").qrcode({
                    width : 200,
                    height : 200,
                    text : "https://open.weixin.qq.com/connect/oauth2/authorize?appid=你的APPID&redirect_uri=你的地址&response_type=code&scope=snsapi_userinfo&connect_redirect=1#wechat_redirect"
                });
            });
        </script>
    </body>
</html>

后端:controller代码

@RequestMapping("/haha")
public String hytg(HttpServletRequest request) {
    //通过前端接口扫描进来可以直接获得code
    String code = request.getParameter("code");
    //通过网页授权接口可以同时获取openid和access_token
    net.sf.json.JSONObject wxInfo = WeixinUtil.getOpenid(code);
    //通过openid和access_token获取用户信息
    net.sf.json.JSONObject wxUserInfo = WeixinUtil.getUserInfo(wxInfo.getString("access_token"), wxInfo.getString("openid"));
    String nickName = wxUserInfo.getString("nickname");
    String sex = wxUserInfo.getString("sex");
    //还想获取哪些信息,自己在下面获取就行然后实现你的逻辑
    return "";
}

WeixinUtil代码

public class WeixinUtil {
    //公众号配置
    public static final String APPID="你的APPID";
    public static final String APPSECRET="你的APPSECRET";
    public static final String URL="你的URL";

    //接口地址
    public static final String GET_OPENID_URL="https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";
    public static final String GET_USERINFO = "https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";


    //通过code获取openid和access_token
    public static JSONObject getApenid(String code){
        Openid open =new Openid();
        String url=GET_OPENID_URL.replace("APPID", APPID).replace("SECRET", APPSECRET).replace("CODE", code);
        JSONObject jsonObject=doGetStr(url);
        return jsonObject;
    }

    //获取授权用户信息
    public static JSONObject getUserInfo (String access_token, String openid) {
	String url = GET_USERINFO.replace("ACCESS_TOKEN", access_token).replace("OPENID", openid);
	JSONObject jsonObject=doGetStr(url);
	return jsonObject;
    }

    public static JSONObject doGetStr(String url){
	DefaultHttpClient httpClient=new  DefaultHttpClient();
	HttpGet httpGet = new HttpGet(url); 
	JSONObject jsonObject=null;
	try {
	    HttpResponse httpResponse=httpClient.execute(httpGet);
	    HttpEntity entity = httpResponse.getEntity();
            if(entity!=null){
	        String result=EntityUtils.toString(entity,"UTF-8");
		jsonObject=JSONObject.fromObject(result);
	    }
	} catch (Exception e) {
	    e.printStackTrace();
	}
	return jsonObject;
    }
}

openid实体类

public class Openid {
    private String accessToken;
    private String expiresIn;
    private String refreshToken;
    private String openid;
    private String scope;
    private String errcode;
    private String errmsg;

    //下面的get、set各位拿eclipse或者idea生成一下吧,都可以快捷直接生成的
}

不出意外的话你就可以获取到用户信息了~

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值