2

I am developing a Xamarin Forms application which integrates Facebook and logs in using the Xamarin.Auth package. But each time a browser window will open to login with Facebook or Twitter.

Does Xamarin support native login with Facebook and Twitter? That is, can I log in using the Facebook or Twitter app on the user's device?

I need to get the Facebook and Twitter id of the user.

Aneesh.A.M
  • 1,128
  • 4
  • 17
  • 35
  • What do you mean by "native login"? That your app asks the username and password, and uses them to log in as the user? Because that is the scenario FB and Twitter *don't* want; they have OAuth as an alternative for that. – S.L. Barth is on codidact.com Nov 30 '16 at 15:28
  • 1
    I think they mean single-sign on instead of web-based OAuth. – valdetero Nov 30 '16 at 15:29
  • Native login means login with device's Facebook or twitter app. – Aneesh.A.M Nov 30 '16 at 15:37
  • Have you seen [this for android](https://forums.xamarin.com/discussion/31928/xamarin-forms-facebook-login-native-login-page-android) and [this for iOS](https://forums.xamarin.com/discussion/59934/facebook-app-login)? Have not tried it yet myself. – hvaughan3 Nov 30 '16 at 16:10
  • Couldn't find anything about Twitter. I need to integrate Twitter and Facebook in similar way. – Aneesh.A.M Nov 30 '16 at 16:29
  • Possible duplicate of [How to login to facebook in Xamarin.Forms](http://stackoverflow.com/questions/24105390/how-to-login-to-facebook-in-xamarin-forms) – jgoldberger - MSFT Nov 30 '16 at 19:56
  • have you tried Xamarin.Social? https://components.xamarin.com/view/xamarin.social . This library works with Facebook, Twitter, Flickr, and App.Net. Also available on Nuget: https://www.nuget.org/packages/Xamarin.Social/ – jgoldberger - MSFT Nov 30 '16 at 19:57
  • Xamarin.Social won't login with native apps. – Aneesh.A.M Dec 01 '16 at 06:05
  • As far as I know, it's not possible to login with native apps. – Elvis Xia - MSFT Dec 01 '16 at 09:57
  • Try this link for facebook. This work for me https://github.com/awslabs/aws-sdk-net-samples/blob/master/XamarinSamples/DynamoDB/ContactManager.Droid/FacebookLoginButtonRenderer.cs – Kirti Zare Dec 01 '16 at 11:39
  • Ah, I misunderstood. Yes, as far as I know you can not use the installed Facebook and Twitter apps for logging in. There is also no guarantee that those apps will be installed on a device, unless you have control of the devices. – jgoldberger - MSFT Dec 06 '16 at 01:42

1 Answers1

0

you can use ACAccountStore and if user doesn't have the social framework you can alternatively use xamarin auth

  ACAccountStore accountStore = new ACAccountStore();
                    ACAccountType accountType = accountStore.FindAccountType(ACAccountType.Facebook);

                    AccountStoreOptions fbAccountStoreOptions = new AccountStoreOptions();
                    fbAccountStoreOptions.FacebookAppId = "333333";
                    fbAccountStoreOptions.SetPermissions(ACFacebookAudience.Everyone, new[] { "email", "user_birthday", "user_about_me","public_profile" });

                    Tuple<bool, NSError> requestResult = await accountStore.RequestAccessAsync(accountType, fbAccountStoreOptions);

                    if (requestResult.Item1)
                    {
                        ACAccount[] availableAccounts = accountStore.Accounts.Where(acco => acco.AccountType.Description == "Facebook").ToArray();
                        int fbAccountsCount = availableAccounts.Count();

                        if (fbAccountsCount < 1)
                        {
                            HandleFacebookAuthorizationUsingOAuthDialog();
                        }
                        else if (fbAccountsCount == 1)
                        {
                            HandleFacebookAuthorizationUsingACAccount(availableAccounts.First());
                        }}
    private void HandleFacebookAuthorizationUsingOAuthDialog()
            {

                try
                {
                    OAuth2Authenticator fbAuthenticator = new OAuth2Authenticator(SharedConstants.FacebookLiveClientId, "email,user_birthday,user_about_me", new Uri("https://m.facebook.com/dialog/oauth/"), new Uri("http://www.facebook.com/connect/login_success.html"));
                    fbAuthenticator.AllowCancel = true;
                    fbAuthenticator.Completed += FbAuthenticator_Completed;
                    fbAuthenticator.Error += FbAuthenticator_Error; ;

                    RootView.PresentViewController(fbAuthenticator.GetUI(), true, null);
                }
                catch (Exception ex)
                {

                }
            }
private async void HandleFacebookAuthorizationUsingACAccount(ACAccount account)
        {

            try
            {
                NSMutableDictionary<NSString, NSString> params_ = new NSMutableDictionary<NSString, NSString>();
                params_.SetValueForKey(new NSString("id,name,birthday,gender"), new NSString("fields"));

                SLRequest request = SLRequest.Create(SLServiceKind.Facebook, SLRequestMethod.Get, new NSUrl($"https://graph.facebook.com/me"), params_);
                request.Account = account ?? throw new ArgumentNullException(nameof(account));

                SLRequestResult response = await request.PerformRequestAsync();

                NSHttpUrlResponse responseData = response.Arg2;
                if (responseData.StatusCode == 200)
                {
                    string jsonResponse = response.Arg1.ToString();
                    FacebookAuthorizationResult authResult = ParseFacebookAuthorizationResultFromJsonResponse(jsonResponse);
                    _facebookAuthTCS?.TrySetResult(new SocailAutheticationResult<FacebookAuthorizationResult>(authResult));
                }
                else
                {
                    _facebookAuthTCS?.TrySetResult(new SocailAutheticationResult<FacebookAuthorizationResult>(SocialAuthorizationState.CouldntConnectToService));
                }

            }

            catch (Exception ex)
            {
                _
            }

        }
Abdullah Tahan
  • 1,963
  • 17
  • 28