微信公眾號(hào) 網(wǎng)頁(yè)授權(quán)登錄及code been used解決詳解
首先微信公眾號(hào)開發(fā)網(wǎng)頁(yè)授權(quán)登錄使用環(huán)境:
開發(fā)工具:eclipse;服務(wù)器:tomcat8,開發(fā)語(yǔ)言:JAVA。
我寫的網(wǎng)頁(yè)授權(quán)登錄時(shí)用開發(fā)者模式自定義view類型按鈕點(diǎn)擊跳轉(zhuǎn)鏈接的。
微信網(wǎng)頁(yè)授權(quán)登錄首先以官方微信開發(fā)文檔為準(zhǔn),大體共分為4步:
先說(shuō)第一步獲取code:
code說(shuō)明:code作為換取access_token的票據(jù),每次用戶授權(quán)帶上的code將不一樣,code只能使用一次,5扽這未被使用自動(dòng)過(guò)期。
微信公眾開發(fā)文檔給的有獲取code的鏈接,建議直接復(fù)制來(lái)用,然后替換其中相應(yīng)的參數(shù)即可。
鏈接為:
其中參數(shù)說(shuō)明:
這官網(wǎng)上都有,這里展示是想說(shuō)明一下scope參數(shù),請(qǐng)注意看官網(wǎng)上給出的demo:
請(qǐng)注意微信授權(quán)登錄scope兩種redirect_url后面跟的鏈接使用的協(xié)議。
這個(gè)協(xié)議使用不當(dāng)可能會(huì)在項(xiàng)目部署到服務(wù)器上測(cè)試時(shí)在安卓和ios上出現(xiàn)問(wèn)題。
至此,以snsapi_base為scope發(fā)起的網(wǎng)頁(yè)授權(quán),是用來(lái)獲取進(jìn)入頁(yè)面的用戶的openid的,并且是靜默授權(quán)并自動(dòng)跳轉(zhuǎn)到回調(diào)頁(yè)的。用戶感知的就是直接進(jìn)入了回調(diào)頁(yè)(往往是業(yè)務(wù)頁(yè)面);
以snsapi_userinfo為scope發(fā)起的網(wǎng)頁(yè)授權(quán),是用來(lái)獲取用戶的基本信息的。但這種授權(quán)需要用戶手動(dòng)同意,并且由于用戶同意過(guò),所以無(wú)須關(guān)注,就可在授權(quán)后獲取該用戶的基本信息。
參數(shù)替換完畢如果以snsapi_userinfo為scope發(fā)起的網(wǎng)頁(yè)授權(quán),是在PC端點(diǎn)擊菜單會(huì)跳出提示用戶同意授權(quán)登錄,如果用戶未關(guān)注公眾號(hào)時(shí)同樣也會(huì)提示,示例頁(yè)面:
如果是在移動(dòng)端用戶關(guān)注情況下則不會(huì)出現(xiàn)此頁(yè)面。
如果用戶同意授權(quán),頁(yè)面將跳轉(zhuǎn)至 redirect_uri/?code=CODE&state=STATE,若跳轉(zhuǎn)錯(cuò)誤請(qǐng)根據(jù)日志輸出的錯(cuò)誤碼在官網(wǎng)上查看相應(yīng)的說(shuō)明,附上官網(wǎng)上錯(cuò)誤返回碼說(shuō)明:
然后是第二步根據(jù)鏈接傳過(guò)來(lái)的code去獲取網(wǎng)頁(yè)授權(quán)access_token:
官網(wǎng)上給出的鏈接:
這個(gè)access_token和基本的access_token不同,具體請(qǐng)參考官網(wǎng)說(shuō)明,這里給出獲取網(wǎng)頁(yè)授權(quán)access_token的JAVA實(shí)現(xiàn)方法:
/** * 獲取網(wǎng)頁(yè)授權(quán)憑證 * * @param appId 公眾賬號(hào)的唯一標(biāo)識(shí) * @param appSecret 公眾賬號(hào)的密鑰 * @param code * @return WeixinAouth2Token */ public static WeixinOauth2Token getOauth2AccessToken(String appId, String appSecret, String code) { WeixinOauth2Token wat = null; // 拼接請(qǐng)求地址 String requestUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code"; requestUrl = requestUrl.replace("APPID", appId); requestUrl = requestUrl.replace("SECRET", appSecret); requestUrl = requestUrl.replace("CODE", code); // 獲取網(wǎng)頁(yè)授權(quán)憑證 JSONObject jsonObject = CommonUtil.httpsRequest(requestUrl, "GET", null); if (null != jsonObject) { try { wat = new WeixinOauth2Token(); wat.setAccessToken(jsonObject.getString("access_token")); wat.setExpiresIn(jsonObject.getInt("expires_in")); wat.setRefreshToken(jsonObject.getString("refresh_token")); wat.setOpenId(jsonObject.getString("openid")); wat.setScope(jsonObject.getString("scope")); } catch (Exception e) { wat = null; int errorCode = jsonObject.getInt("errcode"); String errorMsg = jsonObject.getString("errmsg"); log.error("獲取網(wǎng)頁(yè)授權(quán)憑證失敗 errcode:{} errmsg:{}", errorCode, errorMsg); } } return wat; }
需要的參數(shù)為開發(fā)者ID(AppID),開發(fā)者密碼(AppSecret),和獲取到的code。正確返回json數(shù)據(jù)包為:
然后第三步,如果需要的話進(jìn)行,方法和第二步類似,所需鏈接官網(wǎng)給的有。
最后一步是獲取用戶的信息(需要scope為snsapi_userinfo,snsapi_base只能獲取到用戶的openId):
所需要的請(qǐng)求方法:
http:GET(請(qǐng)使用https協(xié)議) https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN
然后替換成相應(yīng)的參數(shù),JAVA代碼為:
/** * 通過(guò)網(wǎng)頁(yè)授權(quán)獲取用戶信息 * * @param accessToken 網(wǎng)頁(yè)授權(quán)接口調(diào)用憑證 * @param openId 用戶標(biāo)識(shí) * @return SNSUserInfo */ public static SNSUserInfo getSNSUserInfo(String accessToken, String openId) { SNSUserInfo snsUserInfo = null; // 拼接請(qǐng)求地址 String requestUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN"; requestUrl = requestUrl.replace("ACCESS_TOKEN", accessToken).replace("OPENID", openId); // 通過(guò)網(wǎng)頁(yè)授權(quán)獲取用戶信息 JSONObject jsonObject = CommonUtil.httpsRequest(requestUrl, "GET", null); if (null != jsonObject) { try { snsUserInfo = new SNSUserInfo(); // 用戶的標(biāo)識(shí) snsUserInfo.setOpenId(jsonObject.getString("openid")); // 昵稱 snsUserInfo.setNickname(jsonObject.getString("nickname")); // 性別(1是男性,2是女性,0是未知) snsUserInfo.setSex(jsonObject.getInt("sex")); // 用戶所在國(guó)家 snsUserInfo.setCountry(jsonObject.getString("country")); // 用戶所在省份 snsUserInfo.setProvince(jsonObject.getString("province")); // 用戶所在城市 snsUserInfo.setCity(jsonObject.getString("city")); // 用戶頭像 snsUserInfo.setHeadImgUrl(jsonObject.getString("headimgurl")); } catch (Exception e) { snsUserInfo = null; int errorCode = jsonObject.getInt("errcode"); String errorMsg = jsonObject.getString("errmsg"); log.error("獲取用戶信息失敗 errcode:{} errmsg:{}", errorCode, errorMsg); } } return snsUserInfo; }
上面所述皆是根據(jù)微信公眾號(hào)官網(wǎng)以及百度所寫。另外還參考一篇很不錯(cuò)的微信公眾號(hào)開發(fā)文檔,可以說(shuō)是帶我入的門,給個(gè)鏈接:
下面說(shuō)一下微信網(wǎng)頁(yè)授權(quán)登錄中遇到的code been used問(wèn)題:
我在微信網(wǎng)頁(yè)授權(quán)登錄寫完之后開始測(cè)試,在保證代碼的正確性與準(zhǔn)確性后,打開微信公眾號(hào),點(diǎn)擊自己定義跳轉(zhuǎn)鏈接的菜單,并成功進(jìn)入,但是在點(diǎn)擊刷新或者回退是會(huì)報(bào)錯(cuò),錯(cuò)誤的信息就是code been used。
官網(wǎng)上給出的說(shuō)明很詳細(xì),code只能被使用一次,如果顯示code been used則說(shuō)明code被重復(fù)使用了。
首先說(shuō)一個(gè)簡(jiǎn)單的code been used錯(cuò)誤的產(chǎn)生:
有的開發(fā)者在寫網(wǎng)頁(yè)授權(quán)登錄時(shí)會(huì)出現(xiàn)這樣的頁(yè)面:
這是在微信開發(fā)公眾號(hào)上沒(méi)有配置安全域名,導(dǎo)致微信網(wǎng)頁(yè)授權(quán)登錄時(shí)會(huì)顯示這樣的頁(yè)面,url跳轉(zhuǎn)了兩次,傳入的code被重復(fù)使用了,遇到這種的可以現(xiàn)在微信開發(fā)公眾號(hào)里面配置安全域名。
然后說(shuō)普遍遇到的code been used問(wèn)題。
基本思路時(shí):當(dāng)我點(diǎn)擊菜單按鈕進(jìn)入頁(yè)面時(shí),先去sssion緩存中去那由code獲取到的openId,如果openId不存在,則證明code為首次使用,可以根據(jù)傳過(guò)來(lái)的code獲取相應(yīng)的access_token和openId。
如果存在,則直接使用獲取到的openId去獲取用戶的一系列信息。
我用的時(shí)springMVC,簡(jiǎn)單實(shí)現(xiàn)代碼為:
首先在開發(fā)者定義的菜單路徑上配置域名和跳轉(zhuǎn)的控制器方法:
前面模糊的是自己配置的域名,后面/weixin/redirect則是要跳轉(zhuǎn)的方法。
跳轉(zhuǎn)到的方法為:
JAVA代碼:
/**獲取用戶openId * @throws IOException */ @RequestMapping(value = "/redirect", method = RequestMethod.GET) public ModelAndView repairs(ModelAndView mav, HttpServletRequest request, HttpServletResponse resp) throws IOException{ String openId = (String) request.getSession().getAttribute("openId");//先從緩存中獲取通過(guò)code得到的openID System.out.println(openId);//測(cè)試輸出openId if(openId==null){//判斷openId是否為空(判斷code是否為第一次被使用) RedirectUtils.redireUrl1(request, resp);//openid為空也就是code被第一次使用時(shí)跳轉(zhuǎn)方法 return null; } mav.addObject("openId",openId);//沒(méi)有被使用時(shí) mav.setViewName("/weixin/repairs");//返回要跳轉(zhuǎn)的視圖頁(yè)面 return mav; }
RedirectUtils.redireUrl1(request, resp);為重定向跳轉(zhuǎn)的路徑。JAVA代碼:
public static void redireUrl1(HttpServletRequest request,HttpServletResponse response){ System.out.println("跳轉(zhuǎn)");//測(cè)試是否跳轉(zhuǎn)過(guò)來(lái)了 String a=""; if(request.getQueryString()!=null){ a="?"+request.getQueryString(); } String url = Base64.getBase64(request.getRequestURL()+a);//此為鏈接中帶的一些參數(shù) 不需要可以不用寫 System.out.println(request.getRequestURL()+a); String basePath = WeChatConfig.URL+"weixin/wxyz?url="+url;//redirect_uri地址 System.out.println(basePath); String urls="https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + WeChatConfig.APP_ID+ "&redirect_uri=" + CommonUtil.urlEncodeUTF8(basePath)+ "&response_type=code" + "&scope=snsapi_userinfo" + "&state=STATE#wechat_redirect"; try { response.sendRedirect(urls);//重定向執(zhí)行url }catch(Exception e){ e.printStackTrace(); } }
其中Base64.getBase64為Base64加密方法,WeChatConfig.URL為你自己微信公眾平臺(tái)配置的安全域名,CommUtil.urlEncodeUTF8對(duì)重定向鏈接進(jìn)行編碼。
提供一下Base64方法中的加密解密方法,請(qǐng)先下載相應(yīng)的jar包:
** * Base64工具 CREATE 2016.12.14 form yjf * */ public class Base64 { /** * Base64加密 * */ @SuppressWarnings("restriction") public static String getBase64(String value) { byte[] bytes = null; String basevalue = null; try { bytes = value.getBytes("utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } if (bytes != null) { basevalue = new BASE64Encoder().encode(bytes); } return basevalue; } /** * Base64解密 * */ @SuppressWarnings("restriction") public static String getFromBase64(String basevalue) { byte[] bytes = null; String result = null; if (basevalue != null) { BASE64Decoder decoder = new BASE64Decoder(); try { bytes = decoder.decodeBuffer(basevalue); result = new String(bytes, "utf-8"); } catch (Exception e) { e.printStackTrace(); } } return result; } }
然后時(shí)CommUtil.urlEncodeUTF8編碼代碼:
/** * URL編碼(utf-8) * * @param source * @return */ public static String urlEncodeUTF8(String source) { String result = source; try { result = java.net.URLEncoder.encode(source, "utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return result; }
然后方法執(zhí)行response.sendRedirect(urls);跳轉(zhuǎn)回wxyz方法進(jìn)行獲取一系列參數(shù),代碼為:
@RequestMapping(value="/wxyz",method=RequestMethod.GET) public ModelAndView wxYz(ModelAndView mvc,HttpServletRequest req,HttpServletResponse response){ System.out.println("微信驗(yàn)證");//測(cè)試是否跳轉(zhuǎn)到此方法中 String code=req.getParameter("code");//獲取url參數(shù)中的code WeixinOauth2Token weixinOauth2Token = AdvancedUtil.getOauth2AccessToken(WeChatConfig.APP_ID, WeChatConfig.APP_SECRET, code); if(weixinOauth2Token.getOpenId()!=null){ String openId = weixinOauth2Token.getOpenId(); req.getSession().setAttribute("openId",openId);//將獲取到的openID存入session緩存中 System.out.println("openId"+openId); mvc.addObject("openId",openId); mvc.setViewName("redirect:/weixin/wxLogin"); return mvc; } return null; }
其中AdvancedUtil.getOauth2AccessToken方法時(shí)獲取網(wǎng)頁(yè)授權(quán)access_token 方法,代碼為:
/** * 獲取網(wǎng)頁(yè)授權(quán)憑證 * * @param appId 公眾賬號(hào)的唯一標(biāo)識(shí) * @param appSecret 公眾賬號(hào)的密鑰 * @param code * @return WeixinAouth2Token */ public static WeixinOauth2Token getOauth2AccessToken(String appId, String appSecret, String code) { WeixinOauth2Token wat = null; // 拼接請(qǐng)求地址 String requestUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code"; requestUrl = requestUrl.replace("APPID", appId); requestUrl = requestUrl.replace("SECRET", appSecret); requestUrl = requestUrl.replace("CODE", code); // 獲取網(wǎng)頁(yè)授權(quán)憑證 JSONObject jsonObject = CommonUtil.httpsRequest(requestUrl, "GET", null); if (null != jsonObject) { try { wat = new WeixinOauth2Token(); wat.setAccessToken(jsonObject.getString("access_token")); wat.setExpiresIn(jsonObject.getInt("expires_in")); wat.setRefreshToken(jsonObject.getString("refresh_token")); wat.setOpenId(jsonObject.getString("openid")); wat.setScope(jsonObject.getString("scope")); } catch (Exception e) { wat = null; int errorCode = jsonObject.getInt("errcode"); String errorMsg = jsonObject.getString("errmsg"); log.error("獲取網(wǎng)頁(yè)授權(quán)憑證失敗 errcode:{} errmsg:{}", errorCode, errorMsg); } } return wat; }
使用此方法替換上相應(yīng)的參數(shù)即可。因?yàn)槲⑿攀跈?quán)登錄會(huì)跳轉(zhuǎn)兩次鏈接,所以當(dāng)獲取成功則跳轉(zhuǎn)到wxLogin方法中進(jìn)行驗(yàn)證:
String bassPath1 = WeChatConfig.URL+"weixin/wxyz";//定義的路徑 @RequestMapping(value="wxLogin1",method=RequestMethod.GET) public ModelAndView wxLogin(HttpServletRequest request,HttpServletResponse response){ String openId = (String) request.getSession().getAttribute("openId");//先從緩存中去拿openId if(openId==null){//如果沒(méi)有的話 String url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + WeChatConfig.APP_ID + "&redirect_uri=" + CommonUtil.urlEncodeUTF8(bassPath1)+ "&response_type=code" + "&scope=snsapi_userinfo" + "&state=STATE#wechat_redirect"; try { response.sendRedirect(url); } catch (IOException e) { e.printStackTrace(); } }else{ return new ModelAndView("weixin/repairs");//返回頁(yè)面 } return null; }
至此,打開所需要的頁(yè)面,無(wú)論時(shí)第一次進(jìn)入還是刷新 都不會(huì)出現(xiàn)code been used這種情況了,至少本人測(cè)試沒(méi)有出現(xiàn)過(guò)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
idea使用pagehelper實(shí)現(xiàn)后端分頁(yè)功能的步驟詳解
這篇文章主要介紹了idea使用pagehelper實(shí)現(xiàn)后端分頁(yè)功能的步驟,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09Maven dependencyManagement元素標(biāo)簽的具體使用
在Maven中dependencyManagement的作用其實(shí)相當(dāng)于一個(gè)對(duì)所依賴jar包進(jìn)行版本管理的管理器,本文主要介紹了Maven dependencyManagement元素標(biāo)簽的具體使用,感興趣的可以了解一下2024-03-03