/**
* 通过code获取openId
*
* @param code
* @return
*/
@ResponseBody
@RequestMapping(value = "/unvalidate/getOpenIdByCode", method = RequestMethod.POST)
public Map<String, Object> getOpenIdByCode(String code,HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
logger.info("getOpenIdByCode start");
Map<String, Object> resultMap = new HashMap<String, Object>();
String oauth2_url = ConfigUtil.OAUTH2_URL.replace("APPID", ConfigUtil.WAP_APPID)
.replace("SECRET", ConfigUtil.WAP_APP_SECRET).replace("CODE", code);
logger.info("oauth2_url:" + oauth2_url);
net.sf.json.JSONObject jsonObject = CommonUtil.httpsRequestToJsonObject(oauth2_url, "POST", null);
logger.info("jsonObject:" + jsonObject);
Object errorCode = jsonObject.get("errcode");
String openId = "";
if (errorCode != null) {
logger.info("code不合法");
resultMap.put("success", false);
resultMap.put("code", "1");// code不合法
resultMap.put("body", openId);
} else {
openId = jsonObject.getString("openid");
logger.info("openid:" + openId);
resultMap.put("success", true);
resultMap.put("code", "0");// 请求成功
resultMap.put("body", openId);
}
return resultMap;
}
微信公众号:通过code获取openId
最新推荐文章于 2025-02-06 11:26:01 发布
本文详细介绍了如何使用Code参数从微信服务器获取用户的OpenId。通过替换配置文件中的AppId、Secret和Code,构造请求URL,并使用HTTPS请求获取JSON格式的响应,从中解析出OpenId。若Code参数不合法,将返回错误信息。
236

被折叠的 条评论
为什么被折叠?



