微信授权
来源:互联网 发布:金融超市源码 编辑:程序博客网 时间:2024/06/04 19:25
项目写完记录一下微信授权的步骤。欢迎指正交流。
如果有什么不一样以官方文档为主 微信授权官方文档
授权之前别急,要先在微信端配置网页授权回调域名,需要先到公众平台官网中的“开发 - 接口权限 - 网页服务 - 网页帐号 - 网页授权获取用户基本信息”的配置选项中,修改授权回调域名。请注意,这里填写的是域名(是一个字符串),而不是URL,因此请勿加 http:// 等协议头;
- 第一步:用户同意授权,获取code
引导用户进入授权页面,代码如下
@RequestMapping("/login") public void login(HttpServletRequest request,HttpServletResponse response) { //业务参数 String backUrl = "http://xxxxxxxx/xxxx/xxxx/xxx/index.html"; try { //设置回调地址 String redirect_url ="http://xxx.xxx.com/yxx/xxx/callBack"; String url="https://open.weixin.qq.com/connect/oauth2/authorize?" + "appid="+HttpClientUtils.appid + "&redirect_uri="+redirect_url + "&response_type=code" + "&scope=snsapi_userinfo" + "&state="+backUrl+"#wechat_redirect"; response.sendRedirect(url); } catch (Exception e) { logger.error("error------"+e.getMessage()); } }
发送参数的说明见文档
参数说明中redirect_uri标明需要编码,但我的代码中没有进行编码也正确跳转到了我的回调controller里了。
- 第二步:通过code换取网页授权access_token
在用户点击同意授权之后,页面将页面跳转至 redirect_uri/?code=CODE&state=STATE。
这时需要通过code来获取access_token,注意这里的access_token和基础支持中的access_token不同,不需要进行缓存,没有获取次数限制
详细代码如下
@RequestMapping("/callBack") public void callBack1(HttpServletRequest request,HttpServletResponse response){ logger.info("-----------callBack--start-------------"); try { String code = request.getParameter("code"); String backUrl = request.getParameter("state"); String url="https://api.weixin.qq.com/sns/oauth2/access_token" + "?appid="+HttpClientUtils.appid + "&secret="+HttpClientUtils.AppSecret + "&code="+code + "&grant_type=authorization_code"; JSONObject json = HttpClientUtils.doGetJson(url); String openid = json.getString("openid"); String token = json.getString("access_token"); //获取到openid(每个用户的唯一标识)开始处理业务逻辑 UserPo userPo = userService.findUserByToken(openid); if(userPo != null){ response.sendRedirect(backUrl+"?token="+openid); return ; } //如果用户为空 String infoUrl = "https://api.weixin.qq.com/sns/userinfo" + "?access_token="+token + "&openid="+openid + "&lang=zh_CN"; JSONObject userInfo = HttpClientUtils.doGetJson(infoUrl); //将微信信息和用户信息绑定 UserPo user = new UserPo(); //处理微信昵称中有表情的情况(将表情过滤) String name =StringUtil.getName(new String(userInfo.getString("nickname").getBytes("ISO-8859-1"),"UTF-8")); name.replaceAll("[\\ud800\\udc00-\\udbff\\udfff\\ud800-\\udfff]", ""); user.setName(name); user.setToken(userInfo.getString("openid")); user.setImg(userInfo.getString("headimgurl")); user.setSex(userInfo.getInt("sex")); user.setDelMark("0"); userService.insertPO(user); logger.info("----------user is create -----------------"+user.toString()); //新授权用户未绑定手机号,去绑定手机号页面 response.sendRedirect("http://xxxxxxxx/xxxxx/xxxx/xxxx/blind.html?token="+openid); } catch (Exception e) { try { logger.error("----e--"+e.getLocalizedMessage()); request.getRequestDispatcher("/share.jsp").forward(request, response); } catch (Exception ee) { ee.printStackTrace(); } e.printStackTrace(); } return ; }
- 第三步:刷新access_token(如果需要)
这里不需要 - 第四步:拉取用户信息(需scope为 snsapi_userinfo)
通过openId获取用户信息,代码在第二步中已经完成。
好了授权就写好了~
每次看官方文档的时候都是,刚开始看觉得这写的是什么啊,然后找博客看别人怎么写的,各种改,写完之后恍然大悟,奥是这个意思,我好像对官方文档有一种抵触心理。。。。。
阅读全文
0 0
- 微信登录授权
- 微信网页授权
- 微信网页授权
- 微信授权验证
- 微信授权登录
- 微信网页授权
- 微信授权登陆
- 微信授权登录
- 微信授权
- 微信网页授权
- 微信网页授权
- 微信授权登录
- 微信网页授权
- 微信授权
- 微信授权登录
- 微信分享授权
- 微信网页授权
- 微信授权登录
- AlertDialog的普通列表
- strtok
- JavaScript学习
- javascript
- Codeforces 731 B. Coupons and Discounts
- 微信授权
- hdu 4857 拓扑排序
- spring中重要的数据结构
- VR帧传递流程
- Android下拉通知栏会影响Activity的生命周期方法吗?
- 经典算法详解--CART分类决策树、回归树和模型树
- 使用CSS实现两列布局
- 编程问题二:写一个线程池
- nodejs 主子进程通信