微信开放平台开发——网页微信扫码登录(OAuth2.0) http://www.cnblogs.com/0201zcr/p/5133062.html
微信扫描二维码登录网站技术原理 http://www.cnblogs.com/txw1958/p/scan-qrcode-login.html
A: 申请微信开发者账号, 认证, 申请网站用用, 得到 AppID和AppSecret
方式一: 链接方式
B: html增加登录链接
1 2 3 4 |
<a href="https://open.weixin.qq.com/connect/qrconnect?appid=${Appid}&redirect_uri=http://psi.mekau.com/example/wxQRLoginSuccess.do&response_type=code&scope=snsapi_login&state=STATE#wechat_redirect" style="font-size: 20px;"> 微信扫描登录 </a> |
C: 转到这个链接, 会产生一张二维码图片, 用微信扫描, 确认授权登录
方式二: 直接图片方式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
<%@ page import="java.util.Date" %> <%@ page language="java" contentType="text/html;charset=utf-8" pageEncoding="utf-8" %> <% String baseServerUrl = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath(); Date date = new Date(); %> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>获取用户信息1</title> <meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no,minimal-ui"/> <meta name="format-detection" content="email=no"/> <meta name="format-detection" content="address=no"/> <meta name="format-detection" content="telephone=no"/> <script type="text/javascript"> var baseServerUrl = "<%=baseServerUrl%>"; </script> <script src="http://res.wx.qq.com/connect/zh_CN/htmledition/js/wxLogin.js"></script> </head> <body> <div id="stage" style="text-align: center;"> <div id="login_container"></div> </div> <div id="message"></div> <script type="text/javascript"> var obj = new WxLogin({ id:"login_container", appid: "${wxDevAppid}", scope: "snsapi_login", redirect_uri: "http://psi.mekau.com/example/wxQRLoginSuccess.do", state: "", style: "", href: "" }); </script> </body> </html> |
通用后台:
D: 转到redirect_uri这个地址, 拿到code
E: 然后开始读取用户信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
@RequestMapping(value = "/wxQRLoginSuccess.do") public ModelAndView wxQRLoginSuccess(@RequestParam("code") String code, @RequestParam("state") String state, HttpServletRequest request, HttpServletResponse response) { ModelAndView view = new ModelAndView("h5/examples/wxQRLoginSuccess"); Map<String, Object> jsonMap = null; jsonMap = weiXinService.getAccessTokenByCode(wxDevAppid, wxDevSecret, code); System.out.println("返回信息1:" + JSONUtil.toJSONString(jsonMap)); view.addObject("jsonMap", JSONUtil.toJSONString(jsonMap)); String accessToken = null; String openid = null; String refreshToken = null; accessToken = jsonMap.get("access_token").toString(); openid = jsonMap.get("openid").toString(); refreshToken = jsonMap.get("refresh_token").toString(); System.out.println("=============>code :" + code); System.out.println("=============>openid :" + openid); //获得unionId的中文信息 jsonMap = weiXinService.getUnionIdInfoCNByCode(accessToken, openid); System.out.println("=============>返回信息2:" + JSONUtil.toJSONString(jsonMap)); //获得unionId的英文信息 jsonMap = weiXinService.getUnionIdInfoENByCode(accessToken, openid); System.out.println("=============>返回信息3:" + JSONUtil.toJSONString(jsonMap)); //获得userInfo的中文信息 jsonMap = weiXinService.getUserInfoEN(accessToken, openid); System.out.println("=============>返回信息4:" + JSONUtil.toJSONString(jsonMap)); //获得userInfo的英文信息 jsonMap = weiXinService.getUserInfoCN(accessToken, openid); System.out.println("=============>返回信息5:" + JSONUtil.toJSONString(jsonMap)); view.addAllObjects(jsonMap); return view; } |
返回信息:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
返回信息5: { "sex": 1, "nickname": "Pandy", "unionid": "o-fWSwXNwMxEIyOIv3curxeZVNKY", "privilege": [ ], "province": "广东", "openid": "o7rsAw55BNeRJ7T42EaYui9v066o", "language": "zh_CN", "headimgurl": "http://wx.qlogo.cn/mmopen/ESr4jJMEwNsugyDvn5MrDkmH677s8KaW4XgdnKO6QicpM69Jj6JyLY4koVIHVkySRnBJib3F0SRiaOWng8ASoHeWHVMLibyVSCA8/0", "country": "中国", "city": "珠海" } |