protected override void OnElementChanged(ElementChangedEventArgs <Page> e) { base.OnElementChanged(e); var activity = this.Context as Activity; if (!isShown && (Element != null)) { isShown = true; FacebookLogin facebookLogin = (FacebookLogin)Element; var auth = new OAuth2Authenticator( clientId: facebookLogin.FacebookSettings.ClientId, scope: facebookLogin.FacebookSettings.Scope, authorizeUrl: new Uri(facebookLogin.FacebookSettings.AuthorizeUrl), redirectUrl: new Uri(facebookLogin.FacebookSettings.RedirectUrl)); auth.Completed += async(sender, eventArgs) => { string id = string.Empty, token = String.Empty; if (eventArgs.IsAuthenticated) { token = eventArgs.Account.Properties["access_token"]; var request = new OAuth2Request("GET", new Uri("https://graph.facebook.com/me?fields=name"), null, eventArgs.Account); await request.GetResponseAsync().ContinueWith(t => { if (!t.IsFaulted) { var response = t.Result.GetResponseStream(); JsonReader reader = new JsonReader(new InputStreamReader(response, "UTF-8")); reader.BeginObject(); while (reader.HasNext) { var name = reader.NextName(); if (name.Equals("id")) { id = reader.NextString(); break; } else { reader.SkipValue(); } } reader.EndObject(); } }); } facebookLogin.LoginCompleted(eventArgs.IsAuthenticated, id, token); }; activity.StartActivity(auth.GetUI(activity)); } }
public override async void ViewDidAppear(bool animated) { base.ViewDidAppear(animated); if (!isShown && (Element != null)) { isShown = true; FacebookLogin facebookLogin = (FacebookLogin)Element; #if USE_NATIVE_FACEBOOK_LOGIN try { var signedIn = verifyFacebookFromSerttings(); if (string.IsNullOrEmpty(signedIn.Item1) || string.IsNullOrEmpty(signedIn.Item2)) { LoginManager manager = new LoginManager() { LoginBehavior = LoginBehavior.Native }; manager.Init(); LoginManagerLoginResult result = await manager.LogInWithReadPermissionsAsync(new string[] { "public_profile", "email" }, null); var id = string.Empty; var token = string.Empty; var isAuthenticated = !result.IsCancelled && (result.Token != null); if (isAuthenticated) { token = result.Token.TokenString; id = result.Token.UserID; } facebookLogin.LoginCompleted(isAuthenticated, id, token); } else { facebookLogin.LoginCompleted(true, signedIn.Item2, signedIn.Item1); } } catch { facebookLogin.LoginCompleted(false, string.Empty, string.Empty); } #else var auth = new OAuth2Authenticator( clientId: facebookLogin.FacebookSettings.ClientId, scope: facebookLogin.FacebookSettings.Scope, authorizeUrl: new Uri(facebookLogin.FacebookSettings.AuthorizeUrl), redirectUrl: new Uri(facebookLogin.FacebookSettings.RedirectUrl)); auth.Completed += async(sender, eventArgs) => { string id = string.Empty, token = String.Empty; if (eventArgs.IsAuthenticated) { token = eventArgs.Account.Properties["access_token"]; var request = new OAuth2Request("GET", new Uri("https://graph.facebook.com/me?fields=name"), null, eventArgs.Account); await request.GetResponseAsync().ContinueWith(t => { if (!t.IsFaulted) { string response = t.Result.GetResponseText(); NSData responseData = NSData.FromString(response, NSStringEncoding.UTF8); NSError error; NSDictionary jsonDict = (NSDictionary)NSJsonSerialization.Deserialize(responseData, 0, out error); if (jsonDict != null) { NSObject obj = jsonDict.ValueForKey(new NSString("id")); id = obj?.ToString(); } } }); } facebookLogin.LoginCompleted(eventArgs.IsAuthenticated, id, token); }; PresentViewController(auth.GetUI(), true, null); #endif } }