安卓系统上的Unity Firebase Facebook认证



将facebook集成为firebase中的身份验证提供者(已经有了一个有效的gmail实现(。以下代码在Unity中运行良好,FB SDK提示输入令牌,手动输入,令牌被传递到firebase,在控制台中可以看到登录,然后加载所需的场景。

当这是建立在安卓设备上时,完全的行为不会发生,正确的Facebook令牌是从新登录或现有登录中收到的,我们输入SignInWithFacebookOnFirebase函数将此帐户登录到firebase中,没有发生任何其他事情,位于("您的令牌是"+aToken(的调试;

我很确定这与异步行为有关,可能不是在等待任务,但我不确定是什么,任何建议都会很棒!

using System.Collections.Generic;
using UnityEngine;
using Facebook.Unity;
using TMPro;
using UnityEngine.SceneManagement;
using Firebase.Auth;
using Firebase;

public class FacebookAuth : MonoBehaviour
{
private FirebaseAuth auth;
public TMP_Text debug; 
void Awake ()
{
if (FB.IsInitialized) {
FB.ActivateApp();
} else {
//Handle FB.Init
FB.Init( () => {
FB.ActivateApp();
});
}

CheckFirebaseDependencies();
}

private void CheckFirebaseDependencies()
{
FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task =>
{
if (task.IsCompleted)
{
if (task.Result == DependencyStatus.Available)
auth = FirebaseAuth.DefaultInstance;
else
debug.text =("Could not resolve all Firebase dependencies: " + task.Result.ToString());
}
else
{
debug.text =("Dependency check was not completed. Error : " + task.Exception.Message);
}
});
}

void OnApplicationPause (bool pauseStatus)
{
// Check the pauseStatus to see if we are in the foreground
// or background
if (!pauseStatus) {
//app resume
if (FB.IsInitialized) {
FB.ActivateApp();
} else {
//Handle FB.Init
FB.Init( () => {
FB.ActivateApp();
});
}
}
}
private void InitCallBack()
{
if(!FB.IsInitialized)
{
FB.ActivateApp();
}
else
{
//  debug.text=("Failed to initialize");
}
}
private void OnHideUnity(bool isgameshown)
{
if(!isgameshown)
{
Time.timeScale = 0;
}
else
{
Time.timeScale = 1;
}
}    

public void Facebook_Login()
{
var permission = new List<string>() { "public_profile", "email" };
if (!FB.IsLoggedIn)
{
FB.LogInWithReadPermissions(permission, AuthCallBack);
}
else
{
var aToken = AccessToken.CurrentAccessToken.TokenString;
debug.text=("already logged in - starting game" + aToken);  
//THIS IS THE PROBLEM ON ANDROID - ITS NOT HAPPENING/async issue? 
SignInWithFacebookOnFirebase(aToken); 

SceneManager.LoadScene(1);
}
}

public void LogOut()
{
FB.LogOut(); 
debug.text=("Logged out of facebook");
}



private void SignInWithFacebookOnFirebase(string idToken)
{

Firebase.Auth.Credential credential = Firebase.Auth.FacebookAuthProvider.GetCredential(idToken);
auth.SignInWithCredentialAsync(credential).ContinueWith(task => {
if (task.IsCanceled) {
Debug.LogError("SignInWithCredentialAsync was canceled.");
return;
}
if (task.Exception != null) {
Debug.LogWarning("SignInWithCredentialAsync encountered an error: " + task.Exception);

FirebaseException firebaseEx = task.Exception.GetBaseException() as FirebaseException;
AuthError errorCode = (AuthError)firebaseEx.ErrorCode;
string message = "Login Failed!";
switch (errorCode)
{
case AuthError.AccountExistsWithDifferentCredentials:
message = "Your account is already linked to an email address";
break;
//we can add other conditions here if required to catch exceptions 
}
debug.text=(message); 
}
else{


Firebase.Auth.FirebaseUser newUser = task.Result;

Debug.LogFormat("User signed in successfully: {0} ({1})",
newUser.DisplayName, newUser.UserId);
debug.text=("Logged into facebook");  


}
});

}

private void AuthCallBack(ILoginResult result)
{
if(FB.IsLoggedIn)
{
var aToken = result.AccessToken.TokenString;

debug.text=("your token is " + aToken); 
//THIS IS THE PROBLEM ON ANDROID - ITS NOT HAPPENING/async issue? 
SignInWithFacebookOnFirebase(aToken); 
debug.text=("weve signed into firebase");

SceneManager.LoadScene(1);

}
else
{
debug.text=("User Cancelled login");
}
}


}

编辑-所以问题与异步无关,而是firebase凭据在移动设备上被持久化的事实。我在下面取消了我所有已经在工作的谷歌认证代码的注释,我们用我们的脸书信誉登录firebase!因此,当A(用户注销和B(用户关闭应用程序(干净或不干净(时,我需要一些方法来清除我们的此令牌。任何帮助都会很棒!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Firebase;
using Firebase.Auth;
using Google;
using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class GoogleAuth : MonoBehaviour
{  /*
public TMP_Text infoText;
public string webClientId = "<your client id here>";

private FirebaseAuth auth;
private GoogleSignInConfiguration configuration;

private void Awake()
{
configuration = new GoogleSignInConfiguration { WebClientId = webClientId, RequestEmail = true, RequestIdToken = true };
CheckFirebaseDependencies();
}

private void CheckFirebaseDependencies()
{
FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task =>
{
if (task.IsCompleted)
{
if (task.Result == DependencyStatus.Available)
auth = FirebaseAuth.DefaultInstance;
else
AddToInformation("Could not resolve all Firebase dependencies: " + task.Result.ToString());
}
else
{
AddToInformation("Dependency check was not completed. Error : " + task.Exception.Message);
}
});
}

public void SignInWithGoogle() { OnSignIn(); }
public void SignOutFromGoogle() { OnSignOut(); }

private void OnSignIn()
{
GoogleSignIn.Configuration = configuration;
GoogleSignIn.Configuration.UseGameSignIn = false;
GoogleSignIn.Configuration.RequestIdToken = true;
AddToInformation("Calling SignIn");

GoogleSignIn.DefaultInstance.SignIn().ContinueWith(OnAuthenticationFinished);

}

private void OnSignOut()
{
AddToInformation("Calling SignOut");
GoogleSignIn.DefaultInstance.SignOut();

}

public void OnDisconnect()
{

GoogleSignIn.DefaultInstance.Disconnect();
infoText.text=("signed out");
}

internal void OnAuthenticationFinished(Task<GoogleSignInUser> task)
{
if (task.IsFaulted)
{
using (IEnumerator<Exception> enumerator = task.Exception.InnerExceptions.GetEnumerator())
{
if (enumerator.MoveNext())
{
GoogleSignIn.SignInException error = (GoogleSignIn.SignInException)enumerator.Current;
AddToInformation("Got Error: " + error.Status + " " + error.Message);
}
else
{
AddToInformation("Got Unexpected Exception?!?" + task.Exception);
}
}
}
else if (task.IsCanceled)
{
AddToInformation("Cancelled");
}
else
{
AddToInformation("Welcome: " + task.Result.DisplayName + "!");
AddToInformation("Email = " + task.Result.Email);
//AddToInformation("Google ID Token = " + task.Result.IdToken);
AddToInformation("Email = " + task.Result.Email);
SignInWithGoogleOnFirebase(task.Result.IdToken);

}
}

private void SignInWithGoogleOnFirebase(string idToken)
{
Credential credential = GoogleAuthProvider.GetCredential(idToken, null);

auth.SignInWithCredentialAsync(credential).ContinueWith(task =>
{
AggregateException ex = task.Exception;
if (ex != null)
{
if (ex.InnerExceptions[0] is FirebaseException inner && (inner.ErrorCode != 0))
AddToInformation("nError code = " + inner.ErrorCode + " Message = " + inner.Message);
}
else
{
AddToInformation("Sign In Successful.");
SceneManager.LoadScene(1);
}
});
}

public void OnSignInSilently()
{
GoogleSignIn.Configuration = configuration;
GoogleSignIn.Configuration.UseGameSignIn = false;
GoogleSignIn.Configuration.RequestIdToken = true;
AddToInformation("Calling SignIn Silently");

GoogleSignIn.DefaultInstance.SignInSilently().ContinueWith(OnAuthenticationFinished);
}

public void OnGamesSignIn()
{
GoogleSignIn.Configuration = configuration;
GoogleSignIn.Configuration.UseGameSignIn = true;
GoogleSignIn.Configuration.RequestIdToken = false;

AddToInformation("Calling Games SignIn");

GoogleSignIn.DefaultInstance.SignIn().ContinueWith(OnAuthenticationFinished);
}

private void AddToInformation(string str) { infoText.text += "n" + str; } */
}

这个问题已经解决了,因为我正在检查这两个脚本中是否存在firebase依赖关系,并且它抛出了一个异常。我使用logcat logs\adb logcat-s Unity ActivityManager PackageManager dalvikvm DEBUG 找到了这个

最新更新