java实现第三方网页获取微信用户授权后的微信用户基本信息
本文內容基本按照官方文檔,若想直接看官方文檔,可直接點擊查看:https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html
1、測試賬號準備工作
(1)點擊鏈接(https://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index)。使用微信客戶端掃碼登錄即可。
說明:如果自身已存在已認證的微信公眾號,可直接使用自己的公眾進行開發。
(2)關注公眾號
(3)修改回調地址
在網頁服務中,找到“網頁授權獲取用戶基本信息”,點擊“修改”,填寫回調域名,直接填域名即可。
出現如下檢測通過說明域名可用
關于網頁授權回調域名的說明?
1、在微信公眾號請求用戶網頁授權之前,開發者需要先到公眾平臺官網中的“開發 - 接口權限 - 網頁服務 - 網頁帳號 - 網頁授權獲取用戶基本信息”的配置選項中,修改授權回調域名。請注意,這里填寫的是域名(是一個字符串),而不是URL,因此請勿加 http:// 等協議頭;
2、授權回調域名配置規范為全域名,比如需要網頁授權的域名為:www.qq.com,配置以后此域名下面的頁面http://www.qq.com/music.html 、 http://www.qq.com/login.html 都可以進行OAuth2.0鑒權。但http://pay.qq.com 、 http://music.qq.com 、 http://qq.com 無法進行OAuth2.0鑒權
3、如果公眾號登錄授權給了第三方開發者來進行管理,則不必做任何設置,由第三方代替公眾號實現網頁授權即可
?
2、開發流程
?
1 第一步:用戶同意授權,獲取code
2 第二步:通過code換取網頁授權access_token
3 第三步:刷新access_token(如果需要)
4 第四步:拉取用戶信息(需scope為 snsapi_userinfo)
直接擼代碼:
? 說明:示例代碼后臺使用SpringMvc
@RequestMapping(params = "getWxCode", method = RequestMethod.GET)public String wxLogin(HttpServletRequest request, HttpServletResponse response) throws ParseException {// 這個url的域名必須要進行再公眾號中進行注冊驗證,這個地址是成功后的回調地址String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + APPID+ "&redirect_uri=" + REDIRECT_URI + "&response_type=code"+ "&scope=snsapi_userinfo&state=2&connect_redirect=1#wechat_redirect";return "redirect:" + url;// 必須重定向,否則不能成功}其中
@RequestMapping(params = "callBack", method = RequestMethod.GET)public void callBack(ModelMap modelMap, HttpServletRequest req, HttpServletResponse resp) {// 開始獲取微信用戶的基本信息// 獲取codeString code = req.getParameter("code");logger.info("code:" + code);// 通過code換取網頁授權access_tokenString url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + WeiXinUtil.APPID+ "&secret=" + WeiXinUtil.APPSECRET + "&code=" + code + "&grant_type=authorization_code";logger.info("獲取Access_token地址:" + url);JSONObject jsonObject = doGetJson(url);logger.info("回調函數返回內容:" + jsonObject);String openid = jsonObject.getString("openid");String access_token = jsonObject.getString("access_token");String refresh_token = jsonObject.getString("refresh_token");// 驗證access_token是否失效;展示都不需要String chickUrl = "https://api.weixin.qq.com/sns/auth?access_token=" + access_token + "&openid=" + openid;JSONObject chickuserInfo = doGetJson(chickUrl);logger.info("驗證結果:" + chickuserInfo.toString());if(!"0".equals(chickuserInfo.getString("errcode"))){String refreshTokenUrl="https://api.weixin.qq.com/sns/oauth2/refresh_token?appid="+ openid + "&grant_type=refresh_token&refresh_token=" + refresh_token;JSONObject refreshInfo = doGetJson(refreshTokenUrl);logger.info(refreshInfo.toString());access_token = refreshInfo.getString("access_token");}// 獲取用戶信息(需scope為 snsapi_userinfo)String infoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token="+ access_token+ "&openid=" + openid+ "&lang=zh_CN";logger.info("infoUrl:"+infoUrl);JSONObject userInfo = doGetJson(infoUrl);logger.info("用戶基本信息:" + userInfo);}? 其中doGetJson方法是通過網頁獲取網頁返回的json值:以下為參考,可使用其他方式
public static JSONObject doGetJson(String url) {PrintWriter out = null;BufferedReader in = null;String result = "";try {URL realUrl = new URL(url);// 打開和URL之間的連接URLConnection conn = realUrl.openConnection();// 設置通用的請求屬性conn.setRequestProperty("accept", "*/*");conn.setRequestProperty("connection", "Keep-Alive");conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");// 發送POST請求必須設置如下兩行conn.setDoOutput(true);conn.setDoInput(true);// 獲取URLConnection對象對應的輸出流out = new PrintWriter(conn.getOutputStream());// // 發送請求參數// out.print(param);// flush輸出流的緩沖out.flush();// 定義BufferedReader輸入流來讀取URL的響應in = new BufferedReader(new InputStreamReader(conn.getInputStream()));String line;while ((line = in.readLine()) != null) {result += line;}JSONObject jsonObject = JSON.parseObject(result);return jsonObject;} catch (Exception e) {logger.error("獲取失敗:" + e.getMessage());}return null;}?
如有不正確的地方歡迎隨時指出,感恩。?
?
總結
以上是生活随笔為你收集整理的java实现第三方网页获取微信用户授权后的微信用户基本信息的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Redis面试常见问题与详解
- 下一篇: open-capacity-platfo