第三方登陆--微信登陆

微信公众号

如果是自己测试:申请一个测试账号就行了。
具体开发流程参考:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842

定义httpclient用于请求微信接口

public static JSONObject doGetJson(String url) {
        JSONObject jsonObject = null;
        CloseableHttpClient client = null;
        CloseableHttpResponse response = null;
        try {
            //创建HttpClients对象
            client = HttpClients.createDefault();
            // 创建http GET请求
            HttpGet httpGet = new HttpGet(url);
            // 执行请求
            response = client.execute(httpGet);
            int status = response.getStatusLine().getStatusCode();
            if (status >= 200 && status < 300) {
                HttpEntity entity = response.getEntity();
                //转成String
                String result =  entity != null ? EntityUtils.toString(entity,"UTF-8") : null;
                //String转json
                jsonObject = JSON.parseObject(result);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (response != null) response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (client != null) client.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return jsonObject;
    }

用户同意授权,获取code

@GetMapping("/applogin")
    public void applogin(HttpServletResponse response) throws IOException {
        //回调地址
        String backUrl = "http://19278m1w03.imwork.net/weixin/appCallBack";
        /**
         * 参考官网:
         * https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842
         */
        String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + AuthUtil.appID
                + "&redirect_uri=" + URLEncoder.encode(backUrl)
                + "&response_type=code&"
                + "scope=snsapi_userinfo"
                + "&state=STATE"
                + "#wechat_redirect";
        response.sendRedirect(url);
    }

注意:上面的回调地址的域名19278m1w03.imwork.net必须与图片一致
这里写图片描述

注意:appid,secret在这取
这里写图片描述

回调地址接口,获取用户信息:

@GetMapping("/appCallBack")
    public String appCallBack(HttpServletRequest request) throws IOException {
        String code = request.getParameter("code");
        String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + AuthUtil.appID
                + "&secret=" + AuthUtil.appSecret
                + "&code=" + code
                + "&grant_type=authorization_code";
        JSONObject json = AuthUtil.doGetJson(url);
        System.err.println("--------------------------------------------");
        System.out.println(json);
        System.err.println("---------------------------------------------");
        String openid = json.getString("openid");
        String token = json.getString("access_token");
        //拉取用户信息
        String infoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=" + token
                + "&openid=OPENID" + openid
                + "&lang=zh_CN";
        JSONObject infoJson = AuthUtil.doGetJson(infoUrl);
        System.err.println("=============================================");
        System.out.println(infoJson);
        System.err.println("=============================================");
        return "文文是猪";
    }

取到unionid确定用户,注意微信开发平台和微信公众号绑定才会返回unionid。


微信开发平台

微信开发平台开发和公众号完全一致,可能请求接口有变,参考官网:https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419316505&token=&lang=zh_CN

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值