QQ、微信、微博第三方登錄

第三方

WEB開發時常會涉及到第三方登錄的情況,剛剛做了第三方登錄,爲避免忘記做個記錄

環境:windows + jdk1.7 + tomcat1.7
框架:SpringMVC + JPA

準備:

  1. 申請
  2. 配置文件
  3. API

申請成爲開發者

1.新浪:
(1).html及頭部

 <html xmlns:wb="https://open.weibo.com/wb> //html中添加此域
 <meta property="wb:webmaster" content="e07c114e01c43d01"/> //申請測試站時,獲取

(2).配置文件, client_ID和client_SERCRET申請開發站點時獲得

client_ID = x
client_SERCRET = x
redirect_URI = http://www.hejzj.com/sina/loginSinaAction.do
baseURL=https://api.weibo.com/2/
accessTokenURL=https://api.weibo.com/oauth2/access_token
authorizeURL=https://api.weibo.com/oauth2/authorize
rmURL=https://rm.api.weibo.com/2/

(3).官方下載開發包

  //將使用的開發包出去test部分內容,其餘打成jar包。 放入maven倉庫(也可直接引入)
  weibo_4j

2.微信:

(1).申請較爲繁瑣,等待時間長。(個人認證未通過,企業認證可以。 需要提交申請,以及開發者資質申請)

(2).無配置文件

(3).無官方開發包,參照API開發

3.QQ
(1).申請較爲繁瑣(個人認證未通過,企業認證需提交申請)
(2)配置文件(app_ID和app_KEY申請時,獲得)

app_ID = x
app_KEY = x
redirect_URI = http://www.hejzj.com/QQ/afterlogin.do
scope = get_user_info //權限域,有多種設置,目前第三方登錄使用這個足夠
baseURL = https://graph.qq.com/
getUserInfoURL = https://graph.qq.com/user/get_user_info
accessTokenURL = https://graph.qq.com/oauth2.0/token
authorizeURL = https://graph.qq.com/oauth2.0/authorize
getOpenIDURL = https://graph.qq.com/oauth2.0/me
addTopicURL = https://graph.qq.com/shuoshuo/add_topic
addBlogURL = https://graph.qq.com/blog/add_one_blog
addAlbumURL = https://graph.qq.com/photo/add_album
uploadPicURL = https://graph.qq.com/photo/upload_pic
listAlbumURL = https://graph.qq.com/photo/list_album
addShareURL = https://graph.qq.com/share/add_share
checkPageFansURL = https://graph.qq.com/user/check_page_fans
addTURL = https://graph.qq.com/t/add_t
addPicTURL = https://graph.qq.com/t/add_pic_t
delTURL = https://graph.qq.com/t/del_t
getWeiboUserInfoURL = https://graph.qq.com/user/get_info
getWeiboOtherUserInfoURL = https://graph.qq.com/user/get_other_info
getFansListURL = https://graph.qq.com/relation/get_fanslist
getIdolsListURL = https://graph.qq.com/relation/get_idollist
addIdolURL = https://graph.qq.com/relation/add_idol
delIdolURL = https://graph.qq.com/relation/del_idol
getTenpayAddrURL = https://graph.qq.com/cft_info/get_tenpay_addr
getRepostListURL = https://graph.qq.com/t/get_repost_list
version = 2.0.0.0 

(3)開發包:可下載SDK

回調地址

1.新浪
在網站控制檯裏面,設置回調地址在配置文件中:redirect_URI
2.微信
回調地址只需要在申請地址的域名之下,沒有固定寫法
3.QQ
回調地址在配置文件中:redirect_URI

流程

1.新浪

具體說明請查看API

//請求第三方
Oauth oauth = new Oauth();
String url = oauth.authorize("code", ""); //固定傳入code
return "redirect:" + url; //url回調

//回調處理
Oauth oauth = new Oauth();
AccessToken accessToken = oauth.getAccessTokenByCode(code); 
String access_token = accessToken.getAccessToken(); //獲取token
String uid = accessToken.getUserUid(); //新浪用戶唯一標識

Users users = new Users(access_token);
User user = users.showUserById(uid); //獲取用戶信息

2.微信

//連接接口
public static String baseLogin = "https://open.weixin.qq.com/connect/qrconnect?";
//oauth2獲取access_token接口
public static String baseToken = "https://api.weixin.qq.com/sns/oauth2/access_token?";
//獲取用戶信息接口
public static String baseUser = "https://api.weixin.qq.com/sns/userinfo?";
//appId和appSecret申請時獲得
public static String appId = "x";
public static String appSecret = "x";
//回調地址
public static String redirect_uri = "http://www.hejzj.com/weixin/afterlogin.do";
//固定
public static String response_type = "code";
//權限
public static String scope = "snsapi_login";
//固定
public static String grant_type = "authorization_code";

//調用第三方
String url = baseLogin + "appid=" + appId + "&redirect_uri=" + redirect_uri + 
                "&response_type=" + response_type + "&scope=" + scope;

//通過httpClient方法獲取httpEntity
HttpGet get = new HttpGet(url);
CloseableHttpResponse httpResponse = null;

httpResponse = httpClient.execute(get);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
    HttpEntity entity = httpResponse.getEntity(); //封裝了返回的內容
    //轉換成jsonobject
    JSONObject jsonObject = JSONObject.fromObject(EntityUtils.toString(entity));
}

//唯一標識
String openId = jsonObject.getString("openid");
//用戶ID
String unionid = jsonObject.getString("unionid");

//獲取用戶信息接口,再次使用httpClient
String userUrl = baseUser + "access_token=" + accessToken + "&openid=" + openId + "&lang=zh_CN";

get.setURI(new URI(userUrl));       
httpResponse = httpClient.execute(get); 

if (httpResponse.getStatusLine().getStatusCode() == 200) {      
    HttpEntity entity2 = httpResponse.getEntity();
    //此時返回的JsonObject封裝了用戶的信息
    JSONObject jsonObject2 = JSONObject.fromObject(EntityUtils.toString(entity2));
}

3.QQ

//調用第三方
response.sendRedirect(new Oauth().getAuthorizeURL(request));

//回調
AccessToken accessTokenObj = (new Oauth()).getAccessTokenByRequest(request);

//accessToken
accessToken = accessTokenObj.getAccessToken();
//過期時間
tokenExpireIn = accessTokenObj.getExpireIn();

OpenID openIDObj = new OpenID(accessToken);
//唯一標識
openID = openIDObj.getUserOpenID();
UserInfo qzoneUserInfo = new UserInfo(accessToken, openID);
//用戶信息對象
UserInfoBean userInfoBean = qzoneUserInfo.getUserInfo();

其他邏輯屬於業務邏輯

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章