当前位置:   article > 正文

微信公众号--根据用户opneId获取用户信息_根据openid获取用户信息

根据openid获取用户信息

一、登录微信公众号的测试环境,找到“网页授权获取用户基本信息”点击修改,添加上自己的回调地址域名。测试时可以写IP:端口号,正式环境只支持域名不要写http://或https://。

 二、步骤:

 1、用户同意授权,获取code:

      参考链接:

https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxf0e81c3bee622d60&redirect_uri=http%3A%2F%2Fnba.bluewebgame.com%2Foauth_response.php&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect
  • scope的参数有2种:一种是snsapi_base权限只能获取OpenId而且不会弹出授权页面,另为一种是snsapi_userinfo权限会弹出授权页面并且会获取用户信息。
  • redirect_url:是回调地址,当请求成功的时候会重定向到回调页面,并返回code和state。回调地址必须是设置的回调地址域名下且需要进行urlEncode处理。
    如:http://xxxx.xxxx.cn/test/wechat/redirect?code=CODE&state=1

 2、通过code获取access_token

       参考链接:

https://api.weixin.qq.com/sns/oauth2/access_token?appid="+appid+"&secret="+appSecret+"&code="+code+"&grant_type=authorization_code

 3、通过access_token获取用户信息:

       参考链接:

https://api.weixin.qq.com/sns/userinfo?access_token="+access_token+"&openid="+openId+"&lang=en

三、代码

1、通过消息模板推送授权链接:

  1. /**
  2. * 推送给新关注的用户授权
  3. * @param openId 用户的openId
  4. */
  5. public static void followMessage(String openId) {
  6. try {
  7. // 发送消息的时间
  8. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  9. String dateFormat = simpleDateFormat.format(new Date());
  10. // 获取AccessToken的值
  11. String accessToken = WaChatServiceUtil.getAccessToken();
  12. String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken;
  13. // 获取消息模版id
  14. String Message = MessageTemplateEnum.FOLLOW_MESSAGE_TEMPLATE.getKey();
  15. // 回调地址,需要用urlEncode对链接进行处理,微信公众号中需要将授权回调页面域名配置好回调地址需要在回调页面域名下
  16. String callbackUrl = "http%3A%2F%2F"+DOMAINNAME+"%2Fsampling-merchant-web%2Fherman%2Ftest%2Fwechat%2Fredirect.cgi";
  17. // appId
  18. String appId = WaChatServiceUtil.getAPPID();
  19. // 授权地址,用户点击后将进行授权,返回给回调地址code值,state为任意a-zA-Z0-9的参数值,最多128字节
  20. String empowerUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appId + "&redirect_uri=" + callbackUrl + "&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";
  21. // 设置模版消息的内容
  22. WeChatTemplate wc = new WeChatTemplate();
  23. wc.setTouser(openId);
  24. wc.setTemplate_id(Message);
  25. wc.setUrl(empowerUrl);
  26. Map<String, TemplateData> m = new HashMap<>();
  27. m.put("first", new TemplateData("欢迎关注mamain,点击下方进行授权", "#000000"));
  28. m.put("time", new TemplateData(dateFormat, "#000000"));
  29. m.put("url", new TemplateData("点击进行授权,获取您的微信名。", "#173177"));
  30. m.put("remark", new TemplateData("有疑问请联系客服!", "#FF0000"));
  31. wc.setData(m);
  32. //post发送授权消息模版
  33. String rString = WeChatUtils.sendPost(url, JSON.toJSONString(wc));
  34. logger.info("发送授权模版消息结果为:{}", rString);
  35. } catch (Exception e) {
  36. logger.error("发送授权模版消息失败!", e);
  37. }
  38. }

2、获取用户信息

  1. /**
  2. * 回调:微信授权,获取用户code
  3. */
  4. @RequestMapping(value = "wechat/redirect", method = RequestMethod.GET,produces = "text/html; charset=UTF-8")
  5. @ResponseBody
  6. public String wechatRedirect(HttpServletRequest request) throws IOException {
  7. try {
  8. // 获取用户code
  9. String code = request.getParameter("code");
  10. if (null==code){
  11. logger.error("code为空!");
  12. return "error:code为空,授权失败";
  13. }
  14. logger.info("code为{}",code);
  15. // 获取 access_token
  16. String appid= WaChatServiceUtil.getAPPID();
  17. String appSecret=WaChatServiceUtil.getAPPSECRET();
  18. String getToken="https://api.weixin.qq.com/sns/oauth2/access_token?appid="+appid+"&secret="+appSecret+"&code="+code+"&grant_type=authorization_code";
  19. JSONObject token = JSONObject.parseObject(WeChatUtils.sendGet(getToken));
  20. Object access_token=token.get("access_token");
  21. String openId= (String) token.get("openid");
  22. logger.info("access_token:{},openid{}",access_token,openId);
  23. // 获取用户信息
  24. String getUser="https://api.weixin.qq.com/sns/userinfo?access_token="+access_token+"&openid="+openId+"&lang=zh_CN";
  25. JSONObject user = JSONObject.parseObject(WeChatUtils.sendGet(getUser));
  26. String wechatName= (String) user.get("nickname");
  27. logger.info("用户名为:{},openid为:{}",wechatName,openId);
  28. }catch (Exception e){
  29. logger.error("获取用户信息异常!", e);
  30. }
  31. return "<h1 align=\"center\">您已授权成功</h1>";
  32. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/284708
推荐阅读
相关标签
  

闽ICP备14008679号