简体   繁体   中英

Firebase Facebook Authentication with Xamarin Android

I have been trying to implement the logic found on the Firebase documentation on how to authenticate users through Facebook on Firebase. But i looks like it is more focused on native android and not Xamarin. Could anyone help me out with a material? I have searched throughly online and forums for a sample.

Could anyone help me out with a material? I have searched throughly online and forums for a sample.

I didn't find an official tutorial for Xamarin.Android, but I think you can still follow Facebook Login for Android and Authenticate Using Facebook Login on Android to complement it in Xamarin.Android, basically they're pretty like.

First of all, install the Firebase sdks for Xamarin and together with the Xamarin.Facebook.Android .

Then follow the process in the tutorials above.

Here is my demo:

[Activity(Label = "LoginActivity", Exported = true)]
[IntentFilter(new[] { Intent.ActionView },
DataScheme = "@string/fb_login_protocol_scheme"),]
public class LoginActivity : 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.loginlayout);

        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);
    }
}

Layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <com.facebook.login.widget.LoginButton
        android:id="@+id/fblogin"
        android:layout_height="wrap_content"
        android:layout_width="match_parent" />
</LinearLayout>

Manifest:

<uses-permission android:name="android.permission.INTERNET" />
<application android:label="AndroidFireBase">
  <meta-data android:name="com.facebook.sdk.ApplicationId"
      android:value="@string/facebook_app_id" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM