获取微信的openId流程
1.获取微信code
使用接口 : appId 是当前开发者的appId 不是用户的
path 是回调地址
这个链接是授权链接,当重定向这个链接的时候,会展示授权页,点击授权之后 跳入你path的请求接口 回调中带了一个参数code获取到就行
https://open.weixin.qq.com/connect/oauth2/authorize?appid="+appId+"&redirect_uri="+path+"&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect
@RequestMapping(value = "/scanCode") public String scanCode(Integer id, HttpServletRequest request){ String path = AdvancedUtil.authorization(WeiXinConfig.WEB_APPID, Activity.getAuthorizationUrl(request), Activity.getAuthorizationParam(id)); return "redirect:" + path; }
String code = request.getParameter("code");
获取openId和access_token
/** * 获取网页授权凭证 * * @param appId 公众账号的唯一标识 * @param appSecret 公众账号的密钥 * @param code * @return WeixinAouth2Token */ public static WeixinOauth2Token getOauth2AccessToken(String appId, String appSecret, String code) { WeixinOauth2Token wat = null; // 拼接请求地址 String requestUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code"; requestUrl = requestUrl.replace("APPID", appId); requestUrl = requestUrl.replace("SECRET", appSecret); requestUrl = requestUrl.replace("CODE", code); // 获取网页授权凭证 JSONObject jsonObject = CommonUtil.httpsRequest(requestUrl, "GET", null); if (null != jsonObject) { try { wat = new WeixinOauth2Token(); wat.setAccessToken (jsonObject.getString("access_token")); wat.setExpiresIn (jsonObject.getInt("expires_in")); wat.setRefreshToken (jsonObject.getString("refresh_token")); wat.setOpenId (jsonObject.getString("openid")); wat.setScope (jsonObject.getString("scope")); } catch (Exception e) { wat = null; int errorCode = jsonObject.getInt("errcode"); String errorMsg = jsonObject.getString("errmsg"); log.error("获取网页授权凭证失败 errcode:{} errmsg:{}", errorCode, errorMsg); } } return wat; }
/** * 刷新网页授权凭证 * * @param appId 公众账号的唯一标识 * @param refreshToken * @return WeixinAouth2Token */ public static WeixinOauth2Token refreshOauth2AccessToken(String appId, String refreshToken) { WeixinOauth2Token wat = null; // 拼接请求地址 String requestUrl = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=APPID&grant_type=refresh_token&refresh_token=REFRESH_TOKEN"; requestUrl = requestUrl.replace("APPID", appId); requestUrl = requestUrl.replace("REFRESH_TOKEN", refreshToken); // 刷新网页授权凭证 JSONObject jsonObject = CommonUtil.httpsRequest(requestUrl, "GET", null); if (null != jsonObject) { try { wat = new WeixinOauth2Token(); wat.setAccessToken (jsonObject.getString("access_token")); wat.setExpiresIn (jsonObject.getInt("expires_in")); wat.setRefreshToken (jsonObject.getString("refresh_token")); wat.setOpenId (jsonObject.getString("openid")); wat.setScope (jsonObject.getString("scope")); } catch (Exception e) { wat = null; int errorCode = jsonObject.getInt("errcode"); String errorMsg = jsonObject.getString("errmsg"); log.error("刷新网页授权凭证失败 errcode:{} errmsg:{}", errorCode, errorMsg); } } return wat; }
/** * 通过网页授权获取用户信息 * @param accessToken 网页授权接口调用凭证 * @param openId 用户标识 * @return SNSUserInfo */ @SuppressWarnings( { "deprecation", "unchecked" }) public static SNSUserInfo getSNSUserInfo(String accessToken, String openId) { SNSUserInfo snsUserInfo = null; // 拼接请求地址 String requestUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID"; requestUrl = requestUrl.replace("ACCESS_TOKEN", accessToken).replace("OPENID", openId); // 通过网页授权获取用户信息 JSONObject jsonObject = CommonUtil.httpsRequest(requestUrl, "GET", null); if (null != jsonObject) { try { snsUserInfo = new SNSUserInfo(); // 用户的标识 snsUserInfo.setOpenId(jsonObject.getString("openid")); // 昵称 snsUserInfo.setNickname(jsonObject.getString("nickname")); // 性别(1是男性,2是女性,0是未知) snsUserInfo.setSex(jsonObject.getInt("sex")); // 用户所在国家 snsUserInfo.setCountry(jsonObject.getString("country")); // 用户所在省份 snsUserInfo.setProvince(jsonObject.getString("province")); // 用户所在城市 snsUserInfo.setCity(jsonObject.getString("city")); // 用户头像 snsUserInfo.setHeadImgUrl(jsonObject.getString("headimgurl")); // 用户特权信息 snsUserInfo.setPrivilegeList(JSONArray.toList(jsonObject.getJSONArray("privilege"), List.class)); } catch (Exception e) { snsUserInfo = null; int errorCode = jsonObject.getInt("errcode"); String errorMsg = jsonObject.getString("errmsg"); log.error("获取用户信息失败 errcode:{} errmsg:{}", errorCode, errorMsg); } } return snsUserInfo; }
时间: 2024-10-13 01:02:19