第三方qq登入介面
阿新 • • 發佈:2018-12-31
QQ第三方登入
1.首先就是去QQ互聯登入申請了,為什麼要申請呢,是應為他會給你分配一個appid和一個appkey給,不然你是沒有資格去第三方登入的,申請大概一個星期左右
**
唯一需要注意的是第二個 回撥地址了 這個是可以隨時修改的
2.獲取到了appid和appkey然後我們就需要開發後臺了
我們先去獲取java sdk 記得是java sdk不是js sdk
java SDk 下載
解壓:
然後將jar包放入自己的專案
有興趣的可以讀一下 ReadMe.txt 瞭解更詳細 我們繼續下一步
開啟qqconnectconfig.properties
app_ID = 100****(請修改此處)
app_KEY = ******(請修改此處)
redirect_URI = ***********(請修改此處)
scope = get_user_info,add_topic,add_one_blog,add_album,upload_pic,list_album,add_share,check_page_fans,add_t,add_pic_t,del_t,get_repost_list,get_info,get_other_info,get_fanslist,get_idollist,add_idol,del_ido,get_tenpay_addr(請修改此處)
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
上面是需要修改四處的根據自己的資訊來修改
app_ID 申請下來的 appid
app_KEY 申請下來的 appkey
redirect_URI 自己申請時候填寫的回撥地址 所以說
scope
我們一般的話第一個就可以 會獲取到qq的名稱性別和頭像,如果需要其他的話也可以自己去申請 。你自己有什麼許可權的你就可以早scope後面新增
到這裡我們的準備工作就準備好了,開始直接測試吧,
先測試一下自己的介面能不能用
<a href="<%=path%>/qq/QQlogin">請使用你的QQ賬號登陸</a>
<a href="<%=path%>/qq/afterlogin">回撥的測試</a>
@RequestMapping("/qq/QQlogin")
protected void doGet(HttpServletRequest request, HttpServletResponse response, Model model) throws IOException {
response.setContentType("text/html;charset=utf-8");
try {
/* String authorizeURL = new Oauth().getAuthorizeURL(request);*/
response.sendRedirect(new Oauth().getAuthorizeURL(request));
} catch (QQConnectException e) {
e.printStackTrace();
}
}
new Oauth().getAuthorizeURL(request) jar包提供的方法哦 會讀取你在qqconnectconfig.properties 裡面的資訊 然後拼接出來成一URL,具體是什麼樣的可以去官方文件去看,
授權的話就會進去自己在申請時候設定的回調了(redirect_URI)
@RequestMapping("/qq/afterlogin")
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
try {
AccessToken accessTokenObj = (new Oauth()).getAccessTokenByRequest(request);
String accessToken = null,
openID = null;
long tokenExpireIn = 0L;
if (accessTokenObj.getAccessToken().equals("")) {
System.out.print("沒有獲取到響應引數");
}else{
accessToken = accessTokenObj.getAccessToken();
tokenExpireIn = accessTokenObj.getExpireIn();
OpenID openIDObj = new OpenID(accessToken);
openID = openIDObj.getUserOpenID();
UserInfo qzoneUserInfo = new UserInfo(accessToken, openID);
UserInfoBean userInfoBean = qzoneUserInfo.getUserInfo();
}
}catch(Exception e){
e.printStackTrace();
}
return null;
}
謝謝!