How to log in to Facebook in Xamarin.Forms

前端 未结 7 1170
礼貌的吻别
礼貌的吻别 2020-11-27 11:30

I want to make a Xamarin.Forms project, targeting iOS, Android and Windows Phone.

My app needs to authenticate users using Facebook.

Should I implement login

7条回答
  •  旧巷少年郎
    2020-11-27 12:12

    The correct implementation for the Androids PageRenderer is:

    using System;
    using Android.App;
    using Android.Content;
    using OAuth2Demo.XForms.Android;
    using Xamarin.Auth;
    using Xamarin.Forms;
    using Xamarin.Forms.Platform.Android;
    using XamarinAuth;
    
    [assembly: ExportRenderer(typeof(LoginPage), typeof(LoginPageRenderer))]
    
    namespace OAuth2Demo.XForms.Android
    {
        public class LoginPageRenderer : PageRenderer
        {
            public LoginPageRenderer(Context context) : base(context) { }
    
            protected override void OnElementChanged(ElementChangedEventArgs e)
            {
                base.OnElementChanged(e);
    
                // this is a ViewGroup - so should be able to load an AXML file and FindView<>
                var activity = this.Context as Activity;
    
                var auth = new OAuth2Authenticator(
                    clientId: "", // your OAuth2 client id
                    scope: "", // the scopes for the particular API you're accessing, delimited by "+" symbols
                    authorizeUrl: new Uri(""), // the auth URL for the service
                    redirectUrl: new Uri("")); // the redirect URL for the service
    
                auth.Completed += (sender, eventArgs) =>
                {
                    if (eventArgs.IsAuthenticated)
                    {
                        App.SuccessfulLoginAction.Invoke();
                        // Use eventArgs.Account to do wonderful things
                        App.SaveToken(eventArgs.Account.Properties["access_token"]);
                    }
                    else
                    {
                        // The user cancelled
                    }
                };
    
                activity.StartActivity(auth.GetUI(activity));
            }
        }
    }
    

提交回复
热议问题