qq授權登入。微信授權登入、微博授權登入
微信授權登入
1)
微信授權登入相對比較麻煩,你需要到微信公眾平臺去拿到appid、appsecret進行配置;
微信授權有兩種:一種是微信直接授權登入,另外著一種是在該網站你有自己的賬號體系,需要和微信進行繫結;第一種就是下面程式碼出現的;第二種需要得到網站的資料庫,進行繫結,並通過id進行判斷,避免重複;還需要註冊一個花生殼的對映服務得到外網地址;
申請地址: https://open.weixin.qq.com/cgi-bin/index?t=home/index&lang=zh_CN
2)檢視微信提供的介面,寫java程式碼
public void wx() { try { response.sendRedirect("https://open.weixin.qq.com/connect/qrconnect?appid=" + ShareLoginDict.WEIXINKEY.getState() + "&redirect_uri=" + URLEncoder.encode(ShareLoginDict.WEIXINURL.getState()) + "&response_type=code&scope=snsapi_login&state=66666#wechat_redirect"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public Result userwx(String return_code) { Result result = new Result(); Map token = (Map) WeiXinAPI .getToken(return_code); if (token != null && token.get("access_token") != null) { Map user = (Map) WeiXinAPI .getWxUser(token.get("access_token").toString(), token.get("openid").toString()); if (user != null) { result.addModel("openid", user.get("openid")); result.addModel("nickname", user.get("nickname")); result.addModel("headimgurl", user.get("headimgurl")); result.addModel("data", "data_success"); }else{ result.addModel("data", "data_null"); } }else{ result.addModel("data", "data_null"); } return result; }
當用戶通過微信登入時,呼叫微信介面獲取使用者介面返回微信端的openid,暱稱,頭像;然後將此資訊存入到瀏覽器的cookie中,當用戶瀏覽其他資訊時,辨別如果是使用者是用微信登入的,攔截器直接從cookie中獲取使用者的資訊顯示暱稱和頭像,並同時判斷該openid是否和資料庫中使用者做繫結,如果沒有繫結提示使用者註冊。
qq登入
1)qq互聯建立應用
接入QQ登入前,網站需首先進行申請,獲得對應的appid與appkey,以保證後續流程中可正確對網站與使用者進行驗證與授權。
申請appid和appkey的用途
appid :應用的唯一標識。在OAuth2.0認證過程中,appid的值即為oauth_consumer_key的值。
appkey:appid對應的金鑰,訪問使用者資源時用來驗證應用的合法性。在OAuth2.0認證過程中,appkey的值即為oauth_consumer_secret的值。
申請地址: http://connect.qq.com/intro/login/
2)檢視QQ提供的介面,寫java程式碼
public void qq() { try { response.sendRedirect("https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=" + ShareLoginDict.QQKEY.getState() + "&redirect_uri=" + ShareLoginDict.QQURL.getState() + "&scope=get_user_info"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public Result userqq(String return_code) { Result result = new Result(); Map token = (Map) QQAPI .getToken(return_code); if (token != null && token.get("access_token") != null) { Map tokenme = (Map) QQAPI .getTokenMeOpenId(token.get("access_token").toString()); if (tokenme != null && tokenme.get("openid") != null) { Map user = (Map) QQAPI .getQqUser(token.get("access_token").toString(), tokenme.get("openid").toString()); if (user != null) { result.addModel("openid", tokenme.get("openid")); result.addModel("nickname", user.get("nickname")); result.addModel("figureurl", user.get("figureurl")); result.addModel("data", "data_success"); }else{ result.addModel("data", "data_null"); } }else{ result.addModel("data", "data_null"); } }else{ result.addModel("data", "data_null"); } return result; }
當用戶通過QQ登入時,呼叫QQ介面獲取使用者介面返回QQ端的openid,暱稱,頭像;然後將此資訊存入到瀏覽器的cookie中,當用戶瀏覽其他資訊時,辨別如果是使用者是用微信登入的,攔截器直接從cookie中獲取使用者的資訊顯示暱稱和頭像,並同時判斷該openid是否和資料庫中使用者做繫結,如果沒有繫結提示使用者註冊。
3 微博登入
1)微博建立應用
申請地址:http://open.weibo.com/authentication
2)檢視微博提供的介面,寫java程式碼
public void wb() {
try {
response.sendRedirect("https://api.weibo.com/oauth2/authorize?client_id="
+ ShareLoginDict.WEIBOKEY.getState()
+ "&redirect_uri="
+ ShareLoginDict.WEIBOURL.getState()
+ "&response_type=code");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public Result userwb(String return_url, String return_code) {
Result result = new Result();
Map token = (Map) WeiBoAPI.getToken(
return_url, return_code);
if (token != null && token.get("access_token") != null) {
Map user = (Map) WeiBoAPI
.getWbUser(token.get("access_token").toString(),
token.get("uid").toString());
if (user != null) {
result.addModel("name", user.get("screen_name"));
result.addModel("pic", user.get("avatar_large"));
result.addModel("idstr", user.get("idstr"));
result.addModel("data", "data_success");
}else{
result.addModel("data", "data_null");
}
}else{
result.addModel("data", "data_null");
}
return result;
}
當用戶通過微博登入時,呼叫微博介面獲取使用者介面返回微博端的idstr,暱稱,頭像;然後將此資訊存入到瀏覽器的cookie中,當用戶瀏覽其他資訊時,辨別如果是使用者是用微信登入的,攔截器直接從cookie中獲取使用者的資訊顯示暱稱和頭像,並同時判斷該openid是否和資料庫中使用者做繫結,如果沒有繫結提示使用者註冊。
注:本篇文章是在其他網站看到一個大佬寫的,感覺不錯,由於不能轉載,就自己寫了一份,拿來主要是給自己看;微信是自己做過就加了自己的見解,希望對大家有所幫助;