2017-05-31 119 views
2

當人想通過Facebook註冊時,我試圖獲得一個人的名字和姓氏,但想知道是否會有一些安全問題。如何正確使用通過Firebase身份驗證獲得的Facebook令牌?

基礎上document from Firebaseanswer from Stack Overflowexplanation about parameters from Facebook website,我寫了下面的代碼:

firebase.auth().getRedirectResult().then(function(result) { 
    var token = result.credential.accessToken; 
    FB.api('/me', {fields: 'last_name', access_token: token}, function(response) { 
    console.log(response); 
    }) 
}) 

我主要關注的是,根據Facebook的,它說:

一個參數請注意access_token,您可以使用它來使用頁面訪問令牌進行API調用。 應用程序訪問令牌不應該在此SDK中使用,因爲它是客戶端,並且您的應用程序密鑰將被公開。

它看起來像我不能用這種方法來獲取用戶的名字和姓氏。

回答

1

與火力地堡4.0.0開始,額外的IdP數據將直接型UserCredential的結果返回。你不應該需要進行額外的API調用Facebook來得到像第一/姓氏數據:

firebase.auth().getRedirectResult().then(function(result) { 
    if (result.user) { 
    // Additional user info like first name, last name, 
    // Facebook account url, gender, etc. 
    console.log(result.additionalUserInfo.profile); 
    // Facebook access token returned in the process 
    // for the scopes requested. 
    console.log(result.credential.accessToken); 
} 

});

+0

不客氣 – bojeil

+0

你的答案是什麼,我找誰! g爲。謝謝!順便說一下,additionalUserInfo有時可能未定義嗎?我看到console.log的結果裏面有什麼,但additionalUserInfo沒有定義。 – James

+0

我不確定這一點,但我懷疑它可能與我用於註冊的相關: var provider = new firebase.auth.FacebookAuthProvider(); provider.addScope('public_profile'); – James

相關問題