2017-06-20 140 views
0

經過數小時的研究,我一直在努力完成這項工作。我做了進一步的和一些必要的改進,這就是我所擁有的。我可以成功登錄,但接下來我想要做的是讓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 
       { 


       } 
      } 
     } 
    } 

} 

回答

1

我看到你一直在努力,實現IOnCompleteListenerLoginActivity,但我沒有在你的代碼中找到public void OnComplete(Task task),請確保您已經實現了Android.Gms.Tasks.IOnCompleteListener,如果你這樣做,你可以簡單地將您的代碼firebaseAuth.SignInWithCredential(credential).AddOnCompleteListener(this, new IOnCompleteListener<IAuthResult>());更改爲firebaseAuth.SignInWithCredential(credential).AddOnCompleteListener(this, this);

以下是我的演示,它的作品在我身邊:

[Activity(Label = "Activity3", Exported = true)] 
[IntentFilter(new[] { Intent.ActionView }, 
DataScheme = "@string/fb_login_protocol_scheme"),] 
public class Activity3 : Activity, IFacebookCallback, IOnCompleteListener 
{ 
    private ICallbackManager mCallbackManager; 
    private FirebaseAuth mAuth; 

    protected override void OnCreate(Bundle savedInstanceState) 
    { 
     base.OnCreate(savedInstanceState); 

     FacebookSdk.SdkInitialize(this.ApplicationContext); 
     // Create your application here 
     SetContentView(Resource.Layout.layout3); 

     FirebaseApp.InitializeApp(this); 
     mAuth = FirebaseAuth.Instance; 

     LoginButton fblogin = FindViewById<LoginButton>(Resource.Id.fblogin); 
     fblogin.SetReadPermissions("email", "public_profile"); 

     mCallbackManager = CallbackManagerFactory.Create(); 
     fblogin.RegisterCallback(mCallbackManager, this); 
    } 

    private void handleFacebookAccessToken(AccessToken accessToken) 
    { 
     AuthCredential credential = FacebookAuthProvider.GetCredential(accessToken.Token); 
     mAuth.SignInWithCredential(credential).AddOnCompleteListener(this, this); 
    } 

    //facebook IFacebookCallback implementation 
    public void OnSuccess(Java.Lang.Object p0) 
    { 
     LoginResult loginResult = p0 as LoginResult; 
     handleFacebookAccessToken(loginResult.AccessToken); 
    } 

    public void OnCancel() 
    { 
    } 

    public void OnError(FacebookException p0) 
    { 
    } 

    //firebase IOnCompleteListener implementation 
    public void OnComplete(Task task) 
    { 
     if (task.IsSuccessful) 
     { 
      FirebaseUser user = mAuth.CurrentUser; 
     } 
     else 
     { 
      Toast.MakeText(this, "Authentication failed.", ToastLength.Short).Show(); 
     } 
    } 

    // acitivity lifecycle 
    protected override void OnStart() 
    { 
     base.OnStart(); 
     FirebaseUser currentUser = mAuth.CurrentUser; 
    } 

    protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data) 
    { 
     base.OnActivityResult(requestCode, resultCode, data); 
     var resultCodeNum = 0; 
     switch (resultCode) 
     { 
      case Result.Ok: 
       resultCodeNum = -1; 
       break; 

      case Result.Canceled: 
       resultCodeNum = 0; 
       break; 

      case Result.FirstUser: 
       resultCodeNum = 1; 
       break; 
     } 
     mCallbackManager.OnActivityResult(requestCode, resultCodeNum, data); 
    } 
} 
+0

謝謝你這麼mucccchhh!這是你應得的 – Switz