微信公众号开发——获取openId

时间:2025-05-12 09:49:04
/** * * 获取用户openID * @param course * @return * @throws UnsupportedEncodingException */ @RequestMapping(value = "/obtainOpenid" , method = ) public String obtainOpenid(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException{ ServletRequestAttributes attributes = (ServletRequestAttributes) (); request = (); // 获取openid ("text/html"); ("UTF-8"); ("UTF-8"); //String code = ("code");//获取code String code = (); Map<String, String> params = new HashMap<String, String>(); ("appid", ); ("secret",); ("grant_type", "authorization_code"); ("code", code); String result = ( "/sns/oauth2/access_token", params); JSONObject jsonObject = (result); String openid = ("openid").toString(); return openid ; }