2017-08-11 203 views
0

我不明白firebase網站上關於統一firebase Facebook身份驗證的文檔。Unity Firebase facebbok身份驗證

我發現了團結論壇驗證碼:

using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 
using Facebook.Unity; 
using Firebase.Auth; 
using UnityEngine.UI; 

public class facebookAuthenticator : MonoBehaviour 
{ 
public Text userID; 
private FirebaseAuth facebookAuth; 

private void Awake() 
{ 
    if (!FB.IsInitialized) 
     FB.Init(); 
    else 
     FB.ActivateApp(); 
} 

public void logIn() 
{ 
    FB.LogInWithReadPermissions(callback: onLogIn); 
} 

private void onLogIn(ILoginResult result) 
{ 
    if (FB.IsLoggedIn) 
    { 
     AccessToken tocken = AccessToken.CurrentAccessToken; 
     userID.text = tocken.UserId; 
     Credential credential = FacebookAuthProvider.GetCredential(tocken.TokenString); 
    } 
    else 
     Debug.Log("log failed"); 
} 

public void accessToken(Credential firebaseResult) 
{ 
    FirebaseAuth auth = FirebaseAuth.DefaultInstance; 

    if (FB.IsLoggedIn) 
     return; 

    auth.SignInWithCredentialAsync(firebaseResult).ContinueWith(task => 
    { 
     if (task.IsCanceled) 
     { 
      Debug.LogError("SignInWithCredentialAsync was canceled."); 
      return; 
     } 
     if (task.IsFaulted) 
     { 
      Debug.LogError("SignInWithCredentialAsync encountered an error: " + task.Exception); 
      return; 
     } 

     FirebaseUser newUser = task.Result; 
     Debug.LogFormat("User signed in successfully: {0} ({1})", 
      newUser.DisplayName, newUser.UserId); 
    }); 
} 

}

我在我將如何調用此困惑。 執行此代碼之前,我必須執行哪些步驟?我怎麼稱呼它?我只是將它附加到一個按鈕,並給用戶輸入字段填寫?請幫忙,謝謝。

回答

0

我正在做一些非常類似於此的事情,是的,你幾乎必須將logIn函數賦值到一個按鈕中,這樣一旦它被點擊,它就開始了Facebook身份驗證,但對於Firebase部分,您必須獲得訪問權限令牌,並以另一種方式完成。我建議尋找這樣的東西: http://greyzoned.com/tutorials/facebook-sdk-7-3-0-in-unity-5-3-login-username-profile-picture-tutorial/想法如何做到這一點。祝你好運!

+1

它建議包括在回答必要的內容,而不是使用參考鏈接 – Ibo

0

首先,您應該從回調結果中接收訪問令牌。 其次,你應該用你收到accessToken(credential);令牌之後調用火力身份驗證方法,下面的代碼,所以你onLogin方法應該是這樣的

private void onLogIn(ILoginResult result) 
{ 
    if (FB.IsLoggedIn) 
    { 
     AccessToken tocken = result.AccessToken;//received access token 
     userID.text = tocken.UserId; 
     Credential credential = FacebookAuthProvider.GetCredential(tocken.TokenString); 
     accessToken(credential);//invoke auth method 
    } 
    else 
     Debug.Log("log failed"); 
}