微信网页授权提示 errcode:40029 errmsg:invalid code
测试发现是因为不明原因请求两次导致code失效(code只能使用一次)
解决方法:
在返回页面加try catch解决,具体原因稍后待查。
try { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); //网站id String websiteId = request.getParameter("websiteId"); // 用户同意授权后,能获取到code String code = request.getParameter("code"); // 用户同意授权 if (!"authdeny".equals(code)) { // 获取网页授权access_token WeixinOauth2Token weixinOauth2Token = AdvancedUtil.getOauth2AccessToken("XXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXX", code); // 网页授权接口访问凭证 String accessToken = weixinOauth2Token.getAccessToken(); // 用户标识 String openId = weixinOauth2Token.getOpenId(); // 获取用户信息 SNSUserInfo snsUserInfo = AdvancedUtil.getSNSUserInfo(accessToken, openId); String nickName = CommonString.getFormatPara(snsUserInfo.getNickname()); // 设置要传递的参数 request.setAttribute("snsUserInfo", snsUserInfo); request.setAttribute("websiteId", websiteId); } //跳转到index.jsp request.getRequestDispatcher("/index.jsp").forward(request, response); return; } catch (Exception e) { e.printStackTrace(); }