天天看點

QQ微信微網誌聯合登入流程總結

第三方聯合登入一般可以降低網站的獲客成本,是以一般的網站都會做一些聯合登入,常用的就是qq、微信、微網誌了,下面簡單的介紹一下這些聯合登入的方法。

QQ微信微網誌聯合登入流程總結

看了一号店的qq登入源碼可知,點選圖示直接通路的是這個連結

<a href="https://passport.yhd.com/qq/login.do?tp=2.0.0.0.0.lwyye4h-10-c%60toe&amp;ti=h2h3vt">https://passport.yhd.com/qq/login.do?tp=2.0.0.0.0.lwyye4h-10-c`toe&amp;ti=h2h3vt</a>

它首先請求的是自己網站的請求,後面那兩個參數應該沒用,大家自己項目可以不用加,不過抓包能看出來它在服務端做了一個重定向指向了騰訊的qq授權登入頁面

<a href="https://graph.qq.com/oauth2.0/authorize?response_type=code&amp;client_id=100234077&amp;redirect_uri=https://passport.yhd.com/qq/callback.do&amp;scope=get_user_info&amp;state=6172a6f6f311570fe739e96be57b6a67">https://graph.qq.com/oauth2.0/authorize?response_type=code&amp;client_id=100234077&amp;redirect_uri=https://passport.yhd.com/qq/callback.do&amp;scope=get_user_info&amp;state=6172a6f6f311570fe739e96be57b6a67</a>

這裡一号店隻申請了昵稱頭像性别的權限,一般都會同意的。

接下來就是qq的重定向了,qq自己到了這個請求。

<a href="https://graph.qq.com/oauth/show?which=login&amp;display=pc&amp;response_type=code&amp;client_id=100234077&amp;redirect_uri=https://passport.yhd.com/qq/callback.do&amp;scope=get_user_info&amp;state=6172a6f6f311570fe739e96be57b6a67">https://graph.qq.com/oauth/show?which=login&amp;display=pc&amp;response_type=code&amp;client_id=100234077&amp;redirect_uri=https://passport.yhd.com/qq/callback.do&amp;scope=get_user_info&amp;state=6172a6f6f311570fe739e96be57b6a67</a>

但是當qq回調你方法的時候就成了這樣

<a href="https://passport.yhd.com/qq/callback.do?code=78df205e46bfece1d22d31fd77bfd925&amp;state=8625864ecaa9b8424aa40a7e12b4cdb9">https://passport.yhd.com/qq/callback.do?code=78df205e46bfece1d22d31fd77bfd925&amp;state=8625864ecaa9b8424aa40a7e12b4cdb9</a>

在這裡qq加上了一個code參數和state參數,code是用來擷取token值的,隻有10分鐘有效期,state參數是讓你校驗你的請求有沒有被篡改,如果被篡改,你得有相應的處理。在這個回調方法裡,我們得按照qq聯合登入的文檔用申請qq登入所分發的appid和appkey來擷取token,

QQ微信微網誌聯合登入流程總結

擷取完token,你就得按文檔再根據token去擷取使用者的openid

QQ微信微網誌聯合登入流程總結
QQ微信微網誌聯合登入流程總結

當走到這裡時,你已經得到了使用者的openid和其他的使用者資訊,應為openid是唯一的,是以不可能重複,這時候整個聯合登入就已經走完了。你可以根據openid給使用者在你的站内建立一個賬号,設定登入token,認為使用者已經登入了。

qq聯合登入說的這麼清楚了,其實微信聯合登入也是這個套路,具體看文檔調接口就可以了

<a href="https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&amp;t=resource/res_list&amp;verify=1&amp;id=open1419316505&amp;token=%e2%8c%a9=zh_cn">微信聯合登入</a>

<a href="https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&amp;t=resource/res_list&amp;verify=1&amp;id=open1419316518&amp;token=%e2%8c%a9=zh_cn">微信聯合登入查詢使用者資訊</a>

如果你的微信開放平台下有多個應用,你可以用appid+unionid的形式來保證使用者id的唯一性。