- Concise, clear, no bullshit, things did not get off before the micro-letter, get a bad morning with the code be run through.
- Testing framework I use springMVC.
- The need to penetrate the network, please download natapp official website address https://natapp.cn/ , Tutorial: NATAPP1 minute quick novice graphic tutorials
- Need a micro-channel public number, use this demo is to test the number of public access Address: https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login
- Need to focus on your own test public number.
- Please ask about micro-channel authorization https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html
- No public test configuration
- JS interface security domain to fill out your own domain name after the local penetration without http: //
-
Experience Interface permissions table -> Web Services -> Web account or fill out the last step of the domain name without http: //
- The following code starts ↓↓ ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
-
@RequestMapping(params = "toUrl",method=RequestMethod.GET) public void toUrl(HttpServletRequest request,HttpServletResponse response){ StringBuffer sb = new StringBuffer(); //公众号appId String appId = "appidxxxxxxxxxxxxxxx"; //目标方法或者页面 先获取code 换取openId 一个微信用户对应关注的微信号只有一个唯一的openId 在其他关注的公账号下openId不同 String doname = "带http://的域名/xxxxxController.do?getCode"; sb.append("https://open.weixin.qq.com/connect/oauth2/authorize?appid="); sb.append(appId); String url = ""; try { // url = URLEncoder.encode(encodeUrl.toString(), "utf-8"); url = URLEncoder.encode(doname, "utf-8"); sb.append("&redirect_uri=").append(url); //网页授权的静默授权snsapi_base sb.append("&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect"); response.sendRedirect(sb.toString()); } catch (UnsupportedEncodingException e) { System.out.println("重定向url编码失败:>>" + e.getMessage()); e.printStackTrace(); } catch (Exception e) { System.out.println("response重定向失败:>>" + e.getMessage()); e.printStackTrace(); } }
@RequestMapping(params = "getCode",method=RequestMethod.GET) public void toMyPage(HttpServletRequest request,HttpServletResponse response){ //获取重定向携带的code参数值 String code = request.getParameter("code"); String openId = ""; if (StringUtils.isEmpty(openId)) { /* * 根据得到的code参数,内部请求获取openId的方法。 */ openId = getOpenId(request,response,code); } } //发送请求,根据code获取openId public String getOpenId(HttpServletRequest request, HttpServletResponse response,String code) { String content = ""; String openId = ""; String unionId = ""; String appId = "appidxxxxxxxxxxxxxxx";//公众号的appId String appsecret="appsecretxxxxxxxxxxxxx"; //公众号的appsecret //封装获取openId的微信API StringBuffer url = new StringBuffer(); url.append("https://api.weixin.qq.com/sns/oauth2/access_token?appid=") .append(appId) .append("&secret=") .append(appsecret) .append("&code=") .append(code) .append("&grant_type=authorization_code"); ObjectMapper objectMapper = new ObjectMapper(); try { CloseableHttpClient httpClient = HttpClientBuilder.create().build(); HttpGet httpGet = new HttpGet(url.toString()); CloseableHttpResponse re = null; re = httpClient.execute(httpGet); HttpEntity responseEntity = re.getEntity(); content = EntityUtils.toString(responseEntity); Map map = objectMapper.readValue(content, Map.class); openId = String.valueOf(map.get("openid")); unionId = String.valueOf(map.get("unionid")); System.out.println("获取的openID:" + openId); /* * 将openId保存到session中,当其他业务获取openId时, * 可先从session中获取openId. */ request.getSession().setAttribute("openId", openId); /* * 获取到openid就可写绑定用户方法了 */ } catch (JsonParseException e) { System.out.println("json解析失败:"); } catch (JsonMappingException e) { System.out.println("map转换成json失败:"); } catch (Exception e) { System.out.println("http获取openId请求失败:"); } return openId; }
Caishuxueqian brother, want to write to say clearly understand.