H5页面获取微信用户信息操作流程
- (一)获取微信权限,由微信用户确认
- (二)获取相应的ACCESS_TOKEN和OPENID信息
- (三)根据ACCESS_TOKEN和OPENID信息获取相应的用户信息
(一)获取微信权限,由微信用户确认
首先需要准备相应的微信公众号信息,比如appid、appsecret;并且添加当前H5页面的域名地址到微信公众号的“网页授权获取用户基本信息”设置中
设置相应的域名信息
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String qudao= req.getParameter("qudao") == null ? "" : req.getParameter("qudao");
if(JiveGlobals.isEmpty(qudao)){
qudao = "2"; //公众号
}
//微信公众号appid
String appid = "wx333333333333c333";
//微信认证通过后跳转地址,用户获取微信code等信息继续获取ACCESS_TOKEN和OPENID并获取相应用户信息
String tzdz = "http://******/web/oauth";
resp.sendRedirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid="+appid+"&redirect_uri="+tzdz+"?response_type=code&scope=snsapi_userinfo&state=1&connect_redirect=1#wechat_redirect&c="+qudao);
}
(二)获取相应的ACCESS_TOKEN和OPENID信息
微信认证通过后跳转到当前页面,通过code等信息继续获取ACCESS_TOKEN和OPENID
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//得到code
String CODE = req.getParameter("code");
String APPID = "wx333333333333c333"; //公众号appid
String SECRET = "ab5555555555cd55555555555555be"; //公众号SECRET
//换取access_token 其中包含了openid
String URL = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code".replace("APPID", APPID).replace("SECRET", SECRET).replace("CODE", CODE);
GetMethod get = new GetMethod(URL);
get.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"utf-8");
String jsonStr = "";
HttpClient httpclient = new HttpClient();
try {
httpclient.executeMethod(get);
jsonStr = get.getResponseBodyAsString();
} catch (Exception e) {
System.out.println(e);
}
JSONObject jsonObj = JSONObject.fromObject(jsonStr);
String openid = jsonObj.get("openid").toString();
String access_token = jsonObj.get("access_token").toString();
}
(三)根据ACCESS_TOKEN和OPENID信息获取相应的用户信息
微信返回用户信息JSON:
{
"openid": "OPENID",
"nickname": NICKNAME,
"sex": 1,
"province":"PROVINCE",
"city":"CITY",
"country":"COUNTRY",
"headimgurl":"https://thirdwx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/46",
"privilege":[ "PRIVILEGE1" "PRIVILEGE2" ],
"unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL"
}
调用微信接口获取用户信息:
//当上一个页面跳转微信获取scope=snsapi_userinfo权限后可以获取用户昵称头像等信息
String userURL = "https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN".replace("ACCESS_TOKEN", access_token).replace("OPENID", openid);
GetMethod userGet = new GetMethod(userURL);
get.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"utf-8");
String userjsonStr = "";
try {
httpclient.executeMethod(userGet);
userjsonStr = userGet.getResponseBodyAsString();
} catch (Exception e) {
System.out.println(e);
}
JSONObject userjsonObj = JSONObject.fromObject(userjsonStr);
String nickname = String.valueOf(userjsonObj.get("nickname"));
//乱码昵称转码得到正确的昵称
nickname = new String(nickname.getBytes("ISO-8859-1"), "UTF-8");
//微信头像地址
String headimgurl = userjsonObj.get("headimgurl").toString();
相关文章
暂无评论...