首先微信公眾號開發網頁授權登錄使用環境:
開發工具:eclipse;服務器:tomcat8,開發語言:JAVA。
我寫的網頁授權登錄時用開發者模式自定義view類型按鈕點擊跳轉鏈接的。
微信網頁授權登錄首先以官方微信開發文檔為準,大體共分為4步:

先說第一步獲取code:
code說明:code作為換取access_token的票據,每次用戶授權帶上的code將不一樣,code只能使用一次,5扽這未被使用自動過期。
微信公眾開發文檔給的有獲取code的鏈接,建議直接復制來用,然后替換其中相應的參數即可。
鏈接為:
https://open.weixin.qq.com/connect/oauth3/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect
其中參數說明:

這官網上都有,這里展示是想說明一下scope參數,請注意看官網上給出的demo:

請注意微信授權登錄scope兩種redirect_url后面跟的鏈接使用的協議。
這個協議使用不當可能會在項目部署到服務器上測試時在安卓和ios上出現問題。
至此,以snsapi_base為scope發起的網頁授權,是用來獲取進入頁面的用戶的openid的,并且是靜默授權并自動跳轉到回調頁的。用戶感知的就是直接進入了回調頁(往往是業務頁面);
以snsapi_userinfo為scope發起的網頁授權,是用來獲取用戶的基本信息的。但這種授權需要用戶手動同意,并且由于用戶同意過,所以無須關注,就可在授權后獲取該用戶的基本信息。
參數替換完畢如果以snsapi_userinfo為scope發起的網頁授權,是在PC端點擊菜單會跳出提示用戶同意授權登錄,如果用戶未關注公眾號時同樣也會提示,示例頁面:

如果是在移動端用戶關注情況下則不會出現此頁面。
如果用戶同意授權,頁面將跳轉至 redirect_uri/?code=CODE&state=STATE,若跳轉錯誤請根據日志輸出的錯誤碼在官網上查看相應的說明,附上官網上錯誤返回碼說明:

然后是第二步根據鏈接傳過來的code去獲取網頁授權access_token:
官網上給出的鏈接:
https://api.weixin.qq.com/sns/oauth3/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
這個access_token和基本的access_token不同,具體請參考官網說明,這里給出獲取網頁授權access_token的JAVA實現方法:
/**
* 獲取網頁授權憑證
*
* @param appId 公眾賬號的唯一標識
* @param appSecret 公眾賬號的密鑰
* @param code
* @return WeixinAouth3Token
*/
public static WeixinOauth3Token getOauth3AccessToken(String appId, String appSecret, String code) {
WeixinOauth3Token wat = null;
// 拼接請求地址
String requestUrl = "https://api.weixin.qq.com/sns/oauth3/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);
// 獲取網頁授權憑證
JSONObject jsonObject = CommonUtil.httpsRequest(requestUrl, "GET", null);
if (null != jsonObject) {
try {
wat = new WeixinOauth3Token();
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("獲取網頁授權憑證失敗 errcode:{} errmsg:{}", errorCode, errorMsg);
}
}
return wat;
}
需要的參數為開發者ID(AppID),開發者密碼(AppSecret),和獲取到的code。正確返回json數據包為:

然后第三步,如果需要的話進行,方法和第二步類似,所需鏈接官網給的有。
最后一步是獲取用戶的信息(需要scope為snsapi_userinfo,snsapi_base只能獲取到用戶的openId):
所需要的請求方法:
http:GET(請使用https協議) https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN
然后替換成相應的參數,JAVA代碼為:
/**
* 通過網頁授權獲取用戶信息
*
* @param accessToken 網頁授權接口調用憑證
* @param openId 用戶標識
* @return SNSUserInfo
*/
public static SNSUserInfo getSNSUserInfo(String accessToken, String openId) {
SNSUserInfo snsUserInfo = null;
// 拼接請求地址
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);
// 通過網頁授權獲取用戶信息
JSONObject jsonObject = CommonUtil.httpsRequest(requestUrl, "GET", null);
if (null != jsonObject) {
try {
snsUserInfo = new SNSUserInfo();
// 用戶的標識
snsUserInfo.setOpenId(jsonObject.getString("openid"));
// 昵稱
snsUserInfo.setNickname(jsonObject.getString("nickname"));
// 性別(1是男性,2是女性,0是未知)
snsUserInfo.setSex(jsonObject.getInt("sex"));
// 用戶所在國家
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;
}
上面所述皆是根據微信公眾號官網以及百度所寫。另外還參考一篇很不錯的微信公眾號開發文檔,可以說是帶我入的門,給個鏈接:
下面說一下微信網頁授權登錄中遇到的code been used問題:
我在微信網頁授權登錄寫完之后開始測試,在保證代碼的正確性與準確性后,打開微信公眾號,點擊自己定義跳轉鏈接的菜單,并成功進入,但是在點擊刷新或者回退是會報錯,錯誤的信息就是code been used。
官網上給出的說明很詳細,code只能被使用一次,如果顯示code been used則說明code被重復使用了。
首先說一個簡單的code been used錯誤的產生:
有的開發者在寫網頁授權登錄時會出現這樣的頁面:

這是在微信開發公眾號上沒有配置安全域名,導致微信網頁授權登錄時會顯示這樣的頁面,url跳轉了兩次,傳入的code被重復使用了,遇到這種的可以現在微信開發公眾號里面配置安全域名。
然后說普遍遇到的code been used問題。
基本思路時:當我點擊菜單按鈕進入頁面時,先去sssion緩存中去那由code獲取到的openId,如果openId不存在,則證明code為首次使用,可以根據傳過來的code獲取相應的access_token和openId。
如果存在,則直接使用獲取到的openId去獲取用戶的一系列信息。
我用的時springMVC,簡單實現代碼為:
首先在開發者定義的菜單路徑上配置域名和跳轉的控制器方法:

前面模糊的是自己配置的域名,后面/weixin/redirect則是要跳轉的方法。
跳轉到的方法為:
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");//先從緩存中獲取通過code得到的openID
System.out.println(openId);//測試輸出openId
if(openId==null){//判斷openId是否為空(判斷code是否為第一次被使用)
RedirectUtils.redireUrl1(request, resp);//openid為空也就是code被第一次使用時跳轉方法
return null;
}
mav.addObject("openId",openId);//沒有被使用時
mav.setViewName("/weixin/repairs");//返回要跳轉的視圖頁面
return mav;
}
RedirectUtils.redireUrl1(request, resp);為重定向跳轉的路徑。JAVA代碼:
public static void redireUrl1(HttpServletRequest request,HttpServletResponse response){
System.out.println("跳轉");//測試是否跳轉過來了
String a="";
if(request.getQueryString()!=null){
a="?"+request.getQueryString();
}
String url = Base64.getBase64(request.getRequestURL()+a);//此為鏈接中帶的一些參數 不需要可以不用寫
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/oauth3/authorize?appid=" + WeChatConfig.APP_ID+
"&redirect_uri=" + CommonUtil.urlEncodeUTF8(basePath)+
"&response_type=code" +
"&scope=snsapi_userinfo" +
"&state=STATE#wechat_redirect";
try {
response.sendRedirect(urls);//重定向執行url
}catch(Exception e){
e.printStackTrace();
}
}
其中Base64.getBase64為Base64加密方法,WeChatConfig.URL為你自己微信公眾平臺配置的安全域名,CommUtil.urlEncodeUTF8對重定向鏈接進行編碼。
提供一下Base64方法中的加密解密方法,請先下載相應的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;
}
}
然后時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;
}
然后方法執行response.sendRedirect(urls);跳轉回wxyz方法進行獲取一系列參數,代碼為:
@RequestMapping(value="/wxyz",method=RequestMethod.GET)
public ModelAndView wxYz(ModelAndView mvc,HttpServletRequest req,HttpServletResponse response){
System.out.println("微信驗證");//測試是否跳轉到此方法中
String code=req.getParameter("code");//獲取url參數中的code
WeixinOauth3Token weixinOauth3Token = AdvancedUtil.getOauth3AccessToken(WeChatConfig.APP_ID, WeChatConfig.APP_SECRET, code);
if(weixinOauth3Token.getOpenId()!=null){
String openId = weixinOauth3Token.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.getOauth3AccessToken方法時獲取網頁授權access_token 方法,代碼為:
/**
* 獲取網頁授權憑證
*
* @param appId 公眾賬號的唯一標識
* @param appSecret 公眾賬號的密鑰
* @param code
* @return WeixinAouth3Token
*/
public static WeixinOauth3Token getOauth3AccessToken(String appId, String appSecret, String code) {
WeixinOauth3Token wat = null;
// 拼接請求地址
String requestUrl = "https://api.weixin.qq.com/sns/oauth3/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);
// 獲取網頁授權憑證
JSONObject jsonObject = CommonUtil.httpsRequest(requestUrl, "GET", null);
if (null != jsonObject) {
try {
wat = new WeixinOauth3Token();
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("獲取網頁授權憑證失敗 errcode:{} errmsg:{}", errorCode, errorMsg);
}
}
return wat;
}
使用此方法替換上相應的參數即可。因為微信授權登錄會跳轉兩次鏈接,所以當獲取成功則跳轉到wxLogin方法中進行驗證:
String bassPath2 = 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){//如果沒有的話
String url="https://open.weixin.qq.com/connect/oauth3/authorize?appid=" + WeChatConfig.APP_ID +
"&redirect_uri=" + CommonUtil.urlEncodeUTF8(bassPath2)+
"&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");//返回頁面
}
return null;
}
至此,打開所需要的頁面,無論時第一次進入還是刷新 都不會出現code been used這種情況了,至少本人測試沒有出現過。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。