前言
繼關(guān)于服務(wù)端如何接入Apple, Google 的第三方登錄之后,這里介紹在整合Facebook 和Instagram 的第三方登錄時服務(wù)端的驗證工作。
Facebook的登錄驗證與Google相似谆沃,同樣是驗證客戶端通過登錄請求獲取到的登錄token是否有效。
根據(jù)官方文檔 顯示误墓,F(xiàn)acebook的身份驗證有兩種方式:
- 通過code交換
access_token
,然后用獲取的access_token
訪問API驗證客戶端傳來的登錄token是否有效益缎。服務(wù)端收到客戶端傳來的code后谜慌,通過Facebook的API(https://graph.facebook.com/v9.0/oauth/access_token
)交換訪問口令,即access_token莺奔。最后調(diào)用API(https://graph.facebook.com/debug_token?input_token={token-to-inspect}&access_token={app-token-or-admin-token}
)驗證登錄token是否有效欣范。
var (
clientId = "your clientId"
clientSecret = "your clientSecret"
code = "your code"
redirectUri = "your redirectUri"
url = fmt.Sprintf("https://graph.facebook.com/v9.0/oauth/access_token?client_id=%s&redirect_uri=%s&client_secret=%s&code=%s", clientId, redirectUri, clientSecret, code)
)
response, err := http.Get(url)
handleResponse(response)
handleErr(err)
API返回的結(jié)果:
{
"access_token": {訪問口令},
"token_type": {type},
"expires_in": {還有多久過期, 單位: 秒}
}
var (
accessToken = "get from above"
token = "your token"
)
response, err := http.Get(fmt.Sprintf("https://graph.facebook.com/debug_token?input_token=%s&access_token=%s", token, accessToken))
handleResponse(response)
handleErr(err)
- token API
客戶端授權(quán)并取得Facebook返回的登錄token后,將token傳給服務(wù)端令哟,服務(wù)端通過API: https://graph.facebook.com/debug_token?input_token={token-to-inspect}&access_token={app-token-or-admin-token}
其中input_token
為客戶端傳過來的登錄token恼琼,access_token
為API訪問口令,注意這里的access_token
不是通過API獲取的屏富,而是client_id|client_secret
var (
clientId = "your clientId"
clientSecret = "your clientSecret"
token = "your token"
url = fmt.Sprintf("https://graph.facebook.com/debug_token?input_token=%s&access_token=%s|%s", token, clientId, clientSecret)
)
response, err := http.Get(url)
handleResponse(response)
handleErr(err)
具體是code還是token API晴竞,取決于客戶端在用戶點擊登錄按鈕時,客戶端發(fā)起請求中response_type
的值狠半。根據(jù)官網(wǎng)顯示噩死,response_type
的值包括:
值 | 解釋 |
---|---|
code | 響應(yīng)數(shù)據(jù)作為網(wǎng)址參數(shù)納入,且包含 code 參數(shù)(每個登錄請求獨有的加密字符串)神年。如果未指定此參數(shù)已维,這便是默認行為。當服務(wù)器處理口令時已日,這尤其有用 |
token | 響應(yīng)數(shù)據(jù)作為網(wǎng)址片段納入垛耳,且包含訪問口令。桌面應(yīng)用必須為 response_type 選用此設(shè)置飘千。當客戶端處理口令時堂鲜,這尤其有用 |
code%20token | 響應(yīng)數(shù)據(jù)作為網(wǎng)址片段納入,且包含訪問口令和 code 參數(shù) |
granted_scopes | 返回用戶在登錄時授予應(yīng)用的所有權(quán)限的逗號分隔列表占婉∨葑欤可與其他 response_type 值合并甫恩。與 token 合并時逆济,響應(yīng)數(shù)據(jù)作為網(wǎng)址片段納入;與其他值合并時,響應(yīng)數(shù)據(jù)則作為網(wǎng)址參數(shù)納入奖慌。 |
token驗證API返回的結(jié)構(gòu)為:
{
"data": {
"app_id": 138483919580948, //應(yīng)用ID抛虫,等于client_id
"type": "USER",
"application": "Social Cafe",
"expires_at": 1352419328, //token過期時間
"is_valid": true, //是否有效
"issued_at": 1347235328, //什么時候簽發(fā)的
"metadata": {
"sso": "iphone-safari"
},
"scopes": [ //權(quán)限范圍
"email",
"publish_actions"
],
"user_id": "1207059" //用戶在該應(yīng)用下的唯一ID,類似于微信的OpenID
}
}
最后如果需要服務(wù)器自己獲取用戶的頭像简僧、昵稱建椰、性別等信息,需要服務(wù)端通過前面獲取到的access_token
自己去調(diào)用Facebook對應(yīng)的API:
API: https://graph.facebook.com/{facebook_user_id}?fields=name,picture&access_token={access_token}
其中fields
字段根據(jù)自己的需求填寫岛马,用,
分隔棉姐,具體參考官方文檔
根據(jù)Instagram官方文檔 顯示,Instagram不推薦使用Instagram作為身份驗證解決方案啦逆,其推薦使用Facebook登錄, 但是為了調(diào)用Instagram的圖譜API,需要開發(fā)者完成如下準備工作:
- Instagram Business 帳戶 或 Instagram 創(chuàng)作者帳戶
- 與該帳戶相關(guān)聯(lián)的 Facebook 公共主頁
- 一個 Facebook 開發(fā)者帳戶伞矩,可在公共主頁上執(zhí)行任務(wù)
- 已注冊的 Facebook 應(yīng)用 ,且已配置基本設(shè)置
這里介紹如果通過Instagram 圖譜 API 獲取Instagram的訪問口令以及用戶信息
客戶端在App內(nèi)集成Instagram授權(quán)窗口后夏志,在用戶授權(quán)后客戶端將獲取授權(quán)碼乃坤,并將其傳給服務(wù)端,然后客戶端使用該授權(quán)碼來換取訪問口令(access_token)沟蔑。
這里需要注意的是湿诊,Instagram返回的授權(quán)碼包含了后綴#_
,所以使用時需要將#_
去掉才是真正的授權(quán)碼
交換訪問口令的API: https://api.instagram.com/oauth/access_token
, 需要的參數(shù)有:
參數(shù)名 | 參數(shù)值 |
---|---|
client_id | Instagram應(yīng)用ID |
client_secret | 應(yīng)用密鑰 |
redirect_uri | Instagram后臺配置的用戶授權(quán)后的重定向URI |
grant_type | 固定值: authorization_code
|
code | 授權(quán)碼(注意去掉#_ 后綴) |
tokenParam := fmt.Sprintf("client_id=%s&client_secret=%s&grant_type=authorization_code&redirect_uri=%s&code=%s", clientId, clientSecret, redirectUri, code)
response, err := http.Post(tokenUrl, "application/x-www-form-urlencoded", strings.NewReader(tokenParam))
if err != nil {
panic(err)
}
if response.StatusCode != http.StatusOK {
panic(err)
}
返回的結(jié)果格式為:
{
"access_token": "IGQVJ...", //訪問口令
"user_id": 17841405793187218 //Instagram 應(yīng)用中用戶的唯一ID瘦材,類似于微信的OpenID
}
最后使用API: https://graph.instagram.com/{instagram_user_id}?fields=username&access_token={access_token}
查詢用戶節(jié)點厅须,其中fields
字段參考用戶節(jié)點文檔
參考資料
Facebook登錄
手動構(gòu)建登錄流程
Facebook User
Instagram官方文檔
Instagram 圖譜 API
Instagram 用戶節(jié)點文檔