1.首先先註冊賬號,然後填資料審覈,等個三四天吧大概
https://connect.qq.com/
2.審覈通過後點擊創建應用,把備案號什麼的都填(這個審覈兩天之內一般)
!!! 這裏有個重點,網站地址只能填一下,回調地址隨便填一個,通過後可以隨便改的
3. 整理java代碼
我的是springBoot項目 先導入依賴,官網好像沒有java版本的jar包了,我是找個網上座標
<!--QQ登錄的包sdk-->
<dependency>
<groupId>net.gplatform</groupId>
<artifactId>Sdk4J</artifactId>
<version>2.0</version>
</dependency>
然後編寫創建一個配置文件 qqconnectconfig.properties 名字建議一致,改完了以後複製粘貼即可
app_ID = 填成自己的ID
app_KEY = 填成自己的KEY
redirect_URI = 填成自己的回調地址
grant_type=authorization_code
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
前端代碼 這裏的話 根據自己的實際情況
<a href="/qqLogin" onclick="layer.msg('正在通過QQ登入', {icon:16, shade: 0.1, time:0})" >
<img class="avatar size-S" src="img/qq.jpg" title="登陸">登陸
</a>
後端java 跳轉代碼 這個直接複製粘貼記性
//QQ登錄的跳轉
@GetMapping("/qqLogin")
public String qqLogin(HttpServletRequest request, HttpServletResponse response) throws Exception {
response.setContentType("text/html;charset=utf-8");
try {
String authorizeURL = new Oauth().getAuthorizeURL(request);
return "redirect:" + authorizeURL;
} catch (Exception e) {
return null;
}
這個時候網頁是可以用的
然後編寫java的回調代碼
Crotroller類
//QQ登錄的回調
@GetMapping("/qqLoginBack")
public String qqLoginBack(HttpServletRequest request, HttpServletResponse response,HttpSession session) throws Exception {
String code = request.getParameter("code");
//獲取到了用戶信息
Member member = QqService.LoginBack(code);
return "redirect:/";
}
services類,可能是jar版本太老了,jar的方法都不生效了,我就自己看着官方api文檔發請求寫了一個
@PropertySource("classpath:qqconnectconfig.properties")
@Service
public class QQServiceImpl {
@Value("${app_ID}")
private String appID;
@Value("${app_KEY}")
private String appKEY;
@Value("${redirect_URI}")
private String redirectURI;
@Value("${grant_type}")
private String grantType;
@Resource
private MemeberMapper memeber;
//根據code查詢
public Member LoginBack(String code) {
Member member = new Member();
//獲取accessToken
String urlAccessToken = HttpUtil.get("https://graph.qq.com/oauth2.0/token?grant_type=" + grantType + "&client_id=" + appID + "&client_secret=" + appKEY + "&code=" + code + "&redirect_uri=" + redirectURI);
//截取accessToken
String accessToken = StrUtil.subBetween(urlAccessToken, "access_token=", "&expires_in");
if(!StrUtil.isBlank(accessToken)){
//獲取openId
String urlOpenId = HttpUtil.get("https://graph.qq.com/oauth2.0/me?access_token="+accessToken);
//截取openId
String openId = StrUtil.subBetween(urlOpenId, "openid\":\"", "\"}");
if(!StrUtil.isBlank(openId)){
try {
//訪問用戶資料
String userMessage = HttpUtil.get("https://graph.qq.com/user/get_user_info?access_token=" + accessToken + "&oauth_consumer_key="+appID+"&openid=" + openId);
JSONObject jsonObject = JSONUtil.parseObj(userMessage);
//獲取用戶姓名
String nickname = (String)jsonObject.get("nickname");
//獲取QQ頭像
String nickImg = (String)jsonObject.get("figureurl_qq_1");
//獲取QQ性別
String gender= (String)jsonObject.get("gender");
//這裏的隨便怎麼寫
}catch (Exception e){
System.out.println(e);
}
}
}
return member;
}
}
說一下QQ的執行流程吧
https://wiki.connect.qq.com/%E4%BD%BF%E7%94%A8authorization_code%E8%8E%B7%E5%8F%96access_token
主要的執行流程:
QQ頁面登錄後 ===> 會往回調函數裏面穿一個code值, => 根據code值獲取accessToken值 =>再根據accessToken值>獲取openId值
再拿openId值去訪問接口獲取基本信息,這個我得註釋寫的很清楚了哈,具體的流程就是這樣
問題一:,填了回調接口以下,只能回調到服務器上,回調不到本地?
答案:使用 第三方的映射工具,把本地的服務映射成域名.就可以.我在這裏用的是小米球映射工具
問題二:QQ登錄界面出現回調地址校驗通知錯誤?
答案:在本地電腦上host目錄裏面配置一下