經過數小時的研究,我一直在努力完成這項工作。我做了進一步的和一些必要的改進,這就是我所擁有的。我可以成功登錄,但接下來我想要做的是讓Firebase顯示登錄的用戶。這就是我現在一直在努力的幾個小時。Xamarin.Droid與Facebook身份驗證的Firebase
當我從我的代碼firebaseAuth.SignInWithCredential(credential).AddOnCompleteListener(this, new IOnCompleteListener<IAuthResult>());
中取出此行時,我可以成功登錄,但用戶不會顯示在Firebase上。
但是,當我包括行firebaseAuth.SignInWithCredential(credential).AddOnCompleteListener(this, new IOnCompleteListener<IAuthResult>());
,我得到System.NullReferenceException已拋出,我實際上不知道錯誤來自哪裏。請幫忙。
謝謝。
public class LoginActivity : BaseActivity, IFacebookCallback, IAuthStateListener, IOnCompleteListener
{
private ICallbackManager mCallbackManager;
private Firebase.Auth.FirebaseAuth firebaseAuth;
private IOnCompleteListener listener;
public static FirebaseApp app;
private Firebase.Auth.FirebaseAuth.IAuthStateListener firebaseAuthListener;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
FacebookSdk.SdkInitialize(this.ApplicationContext);
LoginButton button = FindViewById<LoginButton>(Resource.Id.login_button);
button.SetReadPermissions("email","public_profile");
mCallbackManager = CallbackManagerFactory.Create();
button.RegisterCallback(mCallbackManager, this);
}
public void handleFacebookAccessToken(AccessToken accessToken)
{
AuthCredential credential = FacebookAuthProvider.GetCredential(accessToken.Token);
firebaseAuth.SignInWithCredential(credential).AddOnCompleteListener(this, new IOnCompleteListener<IAuthResult>());
}
public void OnAuthStateChanged(Firebase.Auth.FirebaseAuth auth)
{
FirebaseUser user = firebaseAuth.CurrentUser;
if (user != null)
{
Console.WriteLine("The current user is " + firebaseAuth.CurrentUser);
}
Console.WriteLine("The current user is " + firebaseAuth.CurrentUser);
}
public void OnCancel()
{
throw new NotImplementedException();
}
public void OnError(FacebookException p0)
{
throw new NotImplementedException();
}
public void OnSuccess(Java.Lang.Object result)
{
LoginResult loginResult = result as LoginResult;
handleFacebookAccessToken(loginResult.AccessToken);
Console.WriteLine("My token is" + loginResult.AccessToken);
}
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
mCallbackManager.OnActivityResult(requestCode, (int)resultCode, data);
}
protected override void OnStart(){
base.OnStart();
// firebaseAuth.AddAuthStateListener(firebaseAuthListener);
}
protected override void OnStop(){
base.OnStop();
// firebaseAuth.RemoveAuthStateListener(firebaseAuthListener);
}
private class IOnCompleteListener<T> : IOnCompleteListener
{
public IntPtr Handle => throw new NotImplementedException();
public void Dispose()
{
throw new NotImplementedException();
}
public void OnComplete(Android.Gms.Tasks.Task task)
{
if (!task.IsSuccessful)
{
}
else
{
}
}
}
}
}
謝謝你這麼mucccchhh!這是你應得的 – Switz