我正在嘗試使用雲功能爲Firebase身份驗證創建包裝REST API。如何在Firebase中爲社交登錄創建包裝REST API?
我怎麼能創建用戶或認證用戶上火力地堡一旦我有(使用Facebook的SDK)在客戶端Facebook的訪問令牌?
我正在嘗試使用雲功能爲Firebase身份驗證創建包裝REST API。如何在Firebase中爲社交登錄創建包裝REST API?
我怎麼能創建用戶或認證用戶上火力地堡一旦我有(使用Facebook的SDK)在客戶端Facebook的訪問令牌?
如果您將Firebase函數與HTTP觸發器一起使用,則可以使用firebase.js客戶端node.js庫對REST API中的用戶進行身份驗證並返回Firbease令牌。您可以將Facebook Access令牌發送到該HTTP端點,使用node.js客戶端庫以signInWithCredential
登錄該用戶,並返回ID令牌和刷新令牌。
如果你想使用REST API:
curl 'https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyAssertion?key=[API_KEY]' \
-H 'Content-Type: application/json' \
--data-binary '{"postBody":"access_token=[FACEBOOK_ACCESS_TOKEN]&providerId=[facebook.com]","requestUri":"[http://localhost]","returnIdpCredential":true,"returnSecureToken":true}'
這將返回火力地堡ID令牌和刷新令牌:
{
"idToken": "[ID_TOKEN]",
"refreshToken": "[REFRESH_TOKEN]",
...
}
這是你所需要的火力地堡驗證會話。
爲了構建用戶,呼叫與該ID令牌以下API:
curl 'https://www.googleapis.com/identitytoolkit/v3/relyingparty/getAccountInfo?key=[API_KEY]' \
-H 'Content-Type: application/json' --data-binary '{"idToken":"[FIREBASE_ID_TOKEN]"}'
這將返回用戶和數據相關聯:
{
"kind": "identitytoolkit#GetAccountInfoResponse",
"users": [
{
"localId": "ZY1rJK0...",
"email": "[email protected]",
"emailVerified": false,
"displayName": "John Doe",
"providerUserInfo": [
{
"providerId": "password",
"displayName": "John Doe",
"photoUrl": "http://localhost:8080/img1234567890/photo.png",
"federatedId": "[email protected]",
"email": "[email protected]",
"rawId": "[email protected]",
"screenName": "[email protected]"
}
],
"photoUrl": "https://lh5.googleusercontent.com/.../photo.jpg",
"passwordHash": "...",
"passwordUpdatedAt": 1.484124177E12,
"validSince": "1484124177",
"disabled": false,
"lastLoginAt": "1484628946000",
"createdAt": "1484124142000",
"customAuth": false
}
]
}
要刷新ID令牌到期後,使用返回的刷新令牌: 使用REST API:
curl 'https://securetoken.googleapis.com/v1/token?key=[API_KEY]' \
-H 'Content-Type: application/x-www-form-urlencoded' \
--data 'grant_type=refresh_token&refresh_token=[REFRESH_TOKEN]'
這將返回一個新的ID令牌和刷新令牌: var firebase = require('firebase');
您發送來自客戶端的FB訪問令牌的HTTP端點和符號:
{
"expires_in": "3600",
"token_type": "Bearer",
"refresh_token": "[REFRESH_TOKEN]",
"id_token": "[ID_TOKEN]",
"user_id": "tRcfmLH7o2XrNELi...",
"project_id": "1234567890"
}
要在後端與客戶端庫使用-in:
var cred = firebase.auth.FacebookAuthProvider.credential(fbAccessToken);
firebase.auth().signInWithCredential(cred).then(function(user) {
// User is obtained here.
// To get refresh token:
// user.refreshToken
// To get ID token:
return user.getIdToken().then(function(idToken) {
// ...
})
}).catch(function(error) {
});
感謝您的回答。你能否提供更多細節?我們不想在前端客戶端上使用任何Firebase庫。是否可以通過Facebook訪問令牌(通過客戶端上的FB SDK檢索)並在Firebase雲功能上完成其他登錄過程? – Ayyappa
雖然我不推薦它,但可以這樣做。老實說,這非常簡單。我將添加HTTP API來調用我的原始答案。 – bojeil