在确保微信公众帐号拥有受权做用域(scope参数)的权限的前提下(服务号得到高级接口后,默认带有scope参数中的snsapi_base和snsapi_userinfo),引导关注者打开以下页面:本做者用菜单的方式引导用户点击进入。json
https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect
个人代码以下:一个Servlet请求 获取codeapi
/** * 根据code取得openId * * @param appid 公众号的惟一标识 * @param secret 公众号的appsecret密钥 * @param code code为换取access_token的票据 * @return */public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //参数 String code = request.getParameter("code"); if(null != code && !"".equals(code)){ log.info("==============[OAuthServlet]获取网页受权code不为空,code="+code); //根据code换取openId OAuthInfo oa = WeixinUtil.getOAuthOpenId(Constants.appId,Constants.appSecret,code); UserInfo info = WeixinUtil.getUserInfo(oa.getAccessToken(), oa.getOpenId()); if(!"".equals(oa) && null != oa){ request.setAttribute("openid", oa.getOpenId()); request.setAttribute("nickname", info.getNickname()); request.getRequestDispatcher("/index.jsp").forward(request, response); }else{ log.info("==============[OAuthServlet]获取网页受权openId失败!"); } }else{ log.info("==============[OAuthServlet]获取网页受权code失败!"); } }
替换相应的APPID APPSECRET SCOPE微信
获取code后,请求如下连接获取access_token: https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
具体作法与上面基本一致。更换相对应的值。须要注意的是code能够写一个Servlet获取。String code = request.getParameter("code");get/post均可以。app
这样子就会返回一下json格式数据jsp
{ "access_token":"ACCESS_TOKEN", "expires_in":7200, "refresh_token":"REFRESH_TOKEN", "openid":"OPENID", "scope":"SCOPE" }
具体代码以下。获取的code换取的access_token post
https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code;
public static OAuthInfo getOAuthOpenId(String appid, String secret, String code ) { OAuthInfo oAuthInfo = null; String o_auth_openid_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code;"; String requestUrl = o_auth_openid_url.replace("APPID", appid).replace("SECRET", secret).replace("CODE", code); JSONObject jsonObject = httpRequest(requestUrl, "GET", null); //oAuthInfo是做者本身把那几个属性参数写在一个类里面了。 // 若是请求成功 if (null != jsonObject) { try { oAuthInfo = new OAuthInfo(); oAuthInfo.setAccessToken(jsonObject.getString("access_token")); oAuthInfo.setExpiresIn(jsonObject.getInt("expires_in")); oAuthInfo.setRefreshToken(jsonObject.getString("refresh_token")); oAuthInfo.setOpenId(jsonObject.getString("openid")); oAuthInfo.setScope(jsonObject.getString("scope")); } catch (JSONException e) { oAuthInfo = null; // 获取token失败 log.error("网页受权获取openId失败 errcode:{} errmsg:{}", jsonObject .getInt("errcode"), jsonObject.getString("errmsg")); } } return oAuthInfo; }
根据上面代码获取的access_token openid 而后再请求获取userinfo的接口。就能获得微信用户的全部信息了。url
这就获取到用户的openid。应用受权做用域,snsapi_base (不弹出受权页面,直接跳转,只能获取用户openid),snsapi_userinfo (弹出受权页面,可经过openid拿到昵称、性别、所在地。而且,即便在未关注的状况下,只要用户受权,也能获取其信息)我本身用的做用域为snsapi_userinfo。用户点击跳转页面为spa
https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect
替换连接里面的大写字母的信息为你本身公众号的。state能够不改。code
写一个Servlet专门接收传递过来的code。进行相应的操做。token
1.OAuthServlet 对code进行access——token的验证
2.一个Servlet的方法调用接口地址。获得相应code。
3.OAuthInfo 返回数据相应的参数的PO类。set/get方法
4.WeiXinUtil添加一个方法 publicOAuth getOAuthInfo(String appid, String secret, String code)获得json格式。并使用JSONObject读取出本身想要的数据。
参考微信文档地址:https://mp.weixin.qq.com/paymch/readtemplate?t=mp/business/course3_tmpl&lang=zh_CN