<html> <head> <title>QQ登錄跳轉(zhuǎn)</title> <script src="http://lib.sinaapp.com/js/jquery/1.7.2/jquery.min.js" type="text/javascript"></script> <script type="text/javascript"> //切割字符串轉(zhuǎn)換參數(shù)表 function toParamMap(str){ var map = {}; var segs = str.split("&"); for(var i in segs){ var seg = segs[i]; var idx = seg.indexOf('='); if(idx < 0){ continue; } var name = seg.substring(0, idx); var value = seg.substring(idx+1); map[name] = value; } return map; } //隱式獲取url響應(yīng)內(nèi)容(JSONP) function openImplict(url){ var script = document.createElement('script'); script.src = url; document.body.appendChild(script); } //獲得openid的回調(diào) function callback(obj) { var openid = obj.openid; $("#openid").text(openid); //跳轉(zhuǎn)服務(wù)端登錄url var resulturl = "@{openapi.QQs.login_result()}"; var accessToken = $("#accessToken").text(); //向服務(wù)端傳輸access_token及openid參數(shù) document.location.href=resulturl + "?access_token=" + accessToken + "&openid=" + openid; } </script> </head> <body> <p>AccessToken:<span id="accessToken"></span>--ExpireIn<span id="expire"></span></p> <p>OpenID:<span id="openid"></span></p> <!-- 執(zhí)行腳本 --> <script type="text/javascript"> //應(yīng)用的APPID var appID = "101207268"; //登錄授權(quán)后的回調(diào)地址,設(shè)置為當(dāng)前url var redirectURI = "@@{openapi.QQs.login()}"; //初始構(gòu)造請(qǐng)求 if (window.location.hash.length == 0) { var path = 'https://graph.qq.com/oauth2.0/authorize?'; var queryParams = ['client_id=' + appID, 'redirect_uri=' + redirectURI, 'scope=' + 'get_user_info,list_album,upload_pic,add_feeds,do_like','response_type=token']; var query = queryParams.join('&'); var url = path + query; window.location.href= url; } //在成功授權(quán)后回調(diào)時(shí)location.hash將帶有access_token信息,開(kāi)始獲取openid else { //獲取access token var accessToken = window.location.hash.substring(1); var map = toParamMap(accessToken); //記錄accessToken $("#accessToken").text(map.access_token); $("#expire").text(map.expires_in); //使用Access Token來(lái)獲取用戶的OpenID var path = "https://graph.qq.com/oauth2.0/me?"; var queryParams = ['access_token='+map.access_token, 'callback=callback']; var query = queryParams.join('&'); var url = path + query; openImplict(url); } </script> </body></html>
https://graph.qq.com/oauth2.0/me?access_token=YOUR_ACCESS_TOKEN
callback( {"client_id":"YOUR_APPID","openid":"YOUR_OPENID"} )
/** * 登錄結(jié)果 * * @param access_token * @param openid */ public static void login_result(String access_token, String openid) { //調(diào)用api獲取qq用戶信息 QQUserInfo user = QQApi.getUserInfo(access_token, openid); //此時(shí)若取得user信息,則可以進(jìn)行保存,并執(zhí)行用戶登錄操作 .... //登錄成功后跳轉(zhuǎn) redirect(xxx); }
/** * QQ互聯(lián)API * * <pre> * 登錄流程: * * 1 前端跳轉(zhuǎn)qq授權(quán)頁(yè)面 * 2 js獲得access_token * 3 通過(guò)jsonp方式獲得openid * 4 server端根據(jù)上傳的access_token及openid獲取用戶信息,如昵稱、頭像 * * 參考文檔: * http://wiki.connect.qq.com/%E5%BC%80%E5%8F%91%E6%94%BB%E7%95%A5_client-side#Step2.EF.BC.9A.E8.8E.B7.E5.8F.96AccessToken * </pre> * * @author xxx * @createDate 2015年3月10日 * */public class QQApi { public static String appId = "xxx"; public static String appSecret = "xxx"; public static String baseUrl = "https://graph.qq.com"; protected static final String URL_GET_USERINFO = baseUrl + "/user/get_user_info?access_token=%s&oauth_consumer_key=%s&openid=%s"; protected static final long ACCESS_TIMEOUT = 15; protected static final String DEF_APP_TOKEN_EXPIRE = "3h"; /** * 獲取用戶信息 * * <pre> * http://wiki.connect.qq.com/get_user_info * * * 調(diào)用地址: * https://graph.qq.com/user/get_user_info * 參數(shù) * access_token=*************& * oauth_consumer_key=12345& * openid * * 返回結(jié)果如下: * { * "ret": 0, * "msg": "", * "is_lost": 0, * "nickname": "小吞", * "gender": "女", * "province": "廣東", * "city": "廣州", * "year": "1993", * "figureurl": "http://qzapp.qlogo.cn/qzapp/101207268/982C9FEADAF7B242C5069B8F390784BF/30", * "figureurl_1": "http://qzapp.qlogo.cn/qzapp/101207268/982C9FEADAF7B242C5069B8F390784BF/50", * "figureurl_2": "http://qzapp.qlogo.cn/qzapp/101207268/982C9FEADAF7B242C5069B8F390784BF/100", * "figureurl_qq_1": "http://q.qlogo.cn/qqapp/101207268/982C9FEADAF7B242C5069B8F390784BF/40", * "figureurl_qq_2": "http://q.qlogo.cn/qqapp/101207268/982C9FEADAF7B242C5069B8F390784BF/100", * "is_yellow_vip": "0", * "vip": "0", * "yellow_vip_level": "0", * "level": "0", * "is_yellow_year_vip": "0" * } * </pre> * * @param accessToken * @return */ public static QQUserInfo getUserInfo(String accessToken, String openid) { if (StringUtils.isEmpty(accessToken) || StringUtils.isEmpty(openid)) { return null; } String url = String.format(URL_GET_USERINFO, accessToken, appId, openid); String resultString = DefaultHttp.get(url, ACCESS_TIMEOUT, GlobalConstants.UTF_8); Logger.debug("[sso-qq]get userinfo. use url '%s'", url); QQUserInfo userinfo = JsonUtil.fromJson(resultString, QQUserInfo.class); if (userinfo == null || !userinfo.hasGot()) { Logger.debug("[sso-qq]get userinfo failed, with result of '%s'", resultString); return null; } Logger.debug("[sso-qq]get userinfo success, with result of '%s'", resultString); return userinfo; }
聯(lián)系客服