/** * 获得授权用户基本信息 * * @param accessToken * @param openId * @return */ public UserInfo getUserInfo(String accessToken, String openId) { Map<String, Object> parameters = new HashMap<>(); parameters.put("access_token", accessToken); parameters.put("openid", openId); parameters.put("lang", "zh_CN"); String url = _USER_INFO_URL_ + "?" + StringUtils.generateQueryString(parameters, false); String result = WebUtils.get(url); UserInfo userInfo = null; if (result != null) { Gson gson = new Gson(); userInfo = gson.fromJson(result, UserInfo.class); } return userInfo; }
/** * 获取用户授权后的访问票据 * * @param code * @return */ public AccessToken getAccessToken(String code) { Map<String, Object> parameters = new HashMap<>(); parameters.put("appid", this.appid); parameters.put("secret", this.appSecret); parameters.put("code", code); parameters.put("grant_type", "authorization_code"); String url = _ACCESS_URL_ + "?" + StringUtils.generateQueryString(parameters, false); String result = WebUtils.get(url); AccessToken accessToken = null; if (result != null) { Gson gson = new Gson(); accessToken = gson.fromJson(result, AccessToken.class); } return accessToken; }