微信网页授权-获取用户信息
第一步:修改网页授权安全域名,什么叫安全域名?安全域名就是说只有这个域名的网页才可以安全的进行网页授权以及获取用户信息。
第二步:下载下这个 MP_verify_Sb2ASLINFP09cMn6.txt(点击下载)放到你的服务器根目录下,可以通过你上面配置的域名直接访问的到,即:http://www.zheyue.me/MP_verify_Sb2ASLINFP09cMn6.txt 可以访问的到。点击确认完成。
第三步: 对自己做的网页地址进行包装,引导客户点击新包装的地址即可。例:
https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxf0e81c3bee622d60&redirect_uri=http%3A%2F%2Fnba.bluewebgame.com%2Foauth_response.php&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect
以上get形式的地址参数的描述:
appid:你微信公众号上的唯一appid(不知道的可以百度一下)
redirect_uri:你的网页url的urlEncode后的字符串。(想把自己的网页地址进行urlEncode可以去站长之家进行转换,具体地址:http://tool.chinaz.com/Tools/URLEncode.aspx)
response_type:写死code不要去动它
scope:填写snsapi_userinfo(如果填写snsapi_base将获取不到用户基本信息,只能获取到openid.但是不会弹出授权界面,属于静默授权,个人认为大部分的人还是希望得到用户基本信息的,所以此处我们填写snsapi_userinfo)。
state:这玩意随便填写,属于预留参数,如果你想进入你的页面的时候可以得到在state上指定的值的话,那就这么做。其实我个人觉得意义不大,如果你真的想得到参数完全可以在 redirect_uri参数的网页地址后面加上?state=State 这样get形式的参数。随意这个参数完全不用理会。
#wechat_redirect:这是最后的尾巴,不要问为什么要加上,反正微信叫我们加我们就加。就这么无意间被强奸了。
第四步:在你的网页上获取用户信息。
这个步骤我封装了一下java代码,大家直接使用就OK,如果需要懂得为什么这么做。可以看看我的代码,毕竟大家都是程序员,看代码比看文章更加直接。放心,我的代码写成了一个公共类,只有一个public的方法,整个类只要调用这一个方法就OK。
主类:调用这个类的 getUserInfo 方法就可以获取用户信息,然后大家自己写servlet或者其他什么玩意和前端对接。
package com.warptor.common; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.warptor.bean.oauth2.AccessTokenBean; import com.warptor.bean.oauth2.WXUserBean; import net.sf.json.JSONObject; public class OAuth2API { private static String appid = ""; //这里填写你自己的appid private static String secret = ""; //这里填写你自己的secret /** * 获取微信用户信息 * @param request * @return */ public static void getUserInfo(HttpServletRequest request,HttpServletResponse response) { response.setHeader("Content-type", "text/html;charset=UTF-8"); response.setCharacterEncoding("UTF-8"); AccessTokenBean atBean = getAccessToken(request); if (!verify(atBean.getAccess_token(), atBean.getOpenid())) { atBean = refreshAccessToken(atBean.getRefresh_token()); } WXUserBean wxub = getUserInfoApi(atBean.getAccess_token(),atBean.getOpenid()); JSONObject jb = JSONObject.fromObject(wxub); try { PrintWriter out = response.getWriter(); out.write(jb.toString()); } catch (IOException e) { e.printStackTrace(); } } /** * 获取AccessToken * @param request * @return */ private static AccessTokenBean getAccessToken(HttpServletRequest request) { String code = request.getParameter("code"); JSONObject jObj = Get.json("https://api.weixin.qq.com/sns/oauth2/access_token?appid="+ appid +"&secret="+ secret +"&code="+ code +"&grant_type=authorization_code"); AccessTokenBean oBean = new AccessTokenBean(); oBean.setAccess_token(jObj.getString("access_token")); oBean.setExpires_in(jObj.getLong("expires_in")); oBean.setRefresh_token(jObj.getString("refresh_token")); oBean.setOpenid(jObj.getString("openid")); oBean.setScope(jObj.getString("scope")); return oBean; } /** * 刷新AccessToken * @param refresh_token * @return */ private static AccessTokenBean refreshAccessToken(String refresh_token) { JSONObject jObj = Get.json("https://api.weixin.qq.com/sns/oauth2/refresh_token?appid="+ appid +"&grant_type=refresh_token&refresh_token="+ refresh_token); AccessTokenBean oBean = new AccessTokenBean(); oBean.setAccess_token(jObj.getString("access_token")); oBean.setExpires_in(jObj.getLong("expires_in")); oBean.setRefresh_token(jObj.getString("refresh_token")); oBean.setOpenid(jObj.getString("openid")); oBean.setScope(jObj.getString("scope")); return oBean; } /** * 验证AccessToken是否可用 * @param accessToken * @param openid * @return */ private static boolean verify(String accessToken,String openid) { JSONObject jObj = Get.json("https://api.weixin.qq.com/sns/auth?access_token="+ accessToken +"&openid=" + openid); return jObj.getInt("errcode") == 0; } /** * 获取用户信息 * @param accessToken * @param openid * @return */ private static WXUserBean getUserInfoApi(String accessToken,String openid) { JSONObject jObj = Get.json("https://api.weixin.qq.com/sns/userinfo?access_token="+ accessToken +"&openid="+ openid +"&lang=zh_CN"); WXUserBean wxub = new WXUserBean(); if (jObj.has("openid")) { wxub.setOpenid(jObj.getString("openid")); } if (jObj.has("nickname")) { wxub.setNickname(jObj.getString("nickname")); } if (jObj.has("sex")) { wxub.setSex(jObj.getInt("sex")); } if (jObj.has("province")) { wxub.setProvince(jObj.getString("province")); } if (jObj.has("city")) { wxub.setCity(jObj.getString("city")); } if (jObj.has("country")) { wxub.setCountry(jObj.getString("country")); } if (jObj.has("headimgurl")) { wxub.setHeadimgurl(jObj.getString("headimgurl")); } if (jObj.has("unionid")) { wxub.setUnionid(jObj.getString("unionid")); } return wxub; } }
Get类:就是主类的依赖类,其中在主类中发现了 Get.json() 这样的方法,就是从这里面来的。主要作用是get形式访问微信接口
package com.warptor.common; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.net.URL; import java.nio.charset.Charset; import net.sf.json.JSONObject; public class Get { private static String readAll(Reader rd) throws IOException { StringBuilder sb = new StringBuilder(); int cp; while ((cp = rd.read()) != -1) { sb.append((char) cp); } return sb.toString(); } public static JSONObject json(String url){ JSONObject jsonObj = null; InputStream is = null; try { is = new URL(url).openStream(); BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8"))); jsonObj = JSONObject.fromObject(readAll(rd)); } catch (Exception e) { e.printStackTrace(); }finally { try { is.close(); } catch (Exception e) { e.printStackTrace(); } } return jsonObj; } }
AccessTokenBean.java : 获取或刷新access_token时,微信接口返回的json结构转换成的实体类
package com.warptor.bean.oauth2; public class AccessTokenBean { private String access_token; private String refresh_token; private String openid; private long expires_in; private String scope; public String getAccess_token() { return access_token; } public void setAccess_token(String access_token) { this.access_token = access_token; } public String getRefresh_token() { return refresh_token; } public void setRefresh_token(String refresh_token) { this.refresh_token = refresh_token; } public String getOpenid() { return openid; } public void setOpenid(String openid) { this.openid = openid; } public long getExpires_in() { return expires_in; } public void setExpires_in(long expires_in) { this.expires_in = expires_in; } public String getScope() { return scope; } public void setScope(String scope) { this.scope = scope; } }
WXUserBean.java :调用微信获取用户信息后返回的json结构转换成的用户信息实体类
package com.warptor.bean.oauth2; public class WXUserBean { private String openid; private String nickname; private int sex; private String province; private String city; private String country; private String headimgurl; private String unionid; public String getOpenid() { return openid; } public void setOpenid(String openid) { this.openid = openid; } public String getNickname() { return nickname; } public void setNickname(String nickname) { this.nickname = nickname; } public int getSex() { return sex; } public void setSex(int sex) { this.sex = sex; } public String getProvince() { return province; } public void setProvince(String province) { this.province = province; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } public String getHeadimgurl() { return headimgurl; } public void setHeadimgurl(String headimgurl) { this.headimgurl = headimgurl; } public String getUnionid() { return unionid; } public void setUnionid(String unionid) { this.unionid = unionid; } }
好了,写完了。欢迎大家一起讨论,我今天下了一个开发小组的手机App。时时刻刻关注着大家。