QQ、微信、微博第三方登入
阿新 • • 發佈:2019-02-17
第三方
WEB開發時常會涉及到第三方登入的情況,剛剛做了第三方登入,為避免忘記做個記錄
環境:windows + jdk1.7 + tomcat1.7
框架:SpringMVC + JPA
準備:
- 申請
- 配置檔案
- 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();
其他邏輯屬於業務邏輯