示例#1
0
        public bool LoginWithSNS(SNSProvider provider)
        {
            try
            {
                oAuth2 = OAuth2ProviderFactory.CreateProvider(provider);

                var authenticator = new OAuth2Authenticator(
                    oAuth2.ClientId,
                    oAuth2.ClientSecret,
                    oAuth2.Scope,
                    oAuth2.AuthorizationUri,
                    oAuth2.RedirectUri,
                    oAuth2.RequestTokenUri,
                    null,
                    oAuth2.IsUsingNativeUI);

                authenticator.Completed += async(s, e) =>
                {
                    //var auth2Authenticator = s as OAuth2Authenticator;
                    if (e.IsAuthenticated)
                    {
                        // If the user is authenticated, request their basic user data from Google
                        // UserInfoUrl = https://www.googleapis.com/oauth2/v2/userinfo
                        var user = await oAuth2.GetUserInfoAsync(e.Account);

                        //AppSettings.User = user;
                        user.SaveUserInfo();
                        MessagingCenter.Send(user, MessengerKeys.AuthenticationRequested, true);
                        //Debug.WriteLine("Authentication Success");
                    }
                };
                authenticator.Error += (s, e) =>
                {
                    //Debug.WriteLine("Authentication error: " + e.Message);
                };

                var presenter = new Xamarin.Auth.Presenters.OAuthLoginPresenter();
                presenter.Login(authenticator);
            }
            catch (Exception ex)
            {
                //Debug.WriteLine("Login Error : " + ex.Message);

                return(false);
            }

            return(true);
        }
        private void ImgBtn_facebookLogin_Clicked(object sender, EventArgs e)
        {
            OAuth2Base oAuth2 = null;

            oAuth2 = FacebookOAuth2.Instance;

            var authenticator = new OAuth2Authenticator(
                oAuth2.ClientId,
                oAuth2.ClientSecret,
                oAuth2.Scope,
                oAuth2.AuthorizationUri,
                oAuth2.RedirectUri,
                oAuth2.RequestTokenUri,
                null,
                oAuth2.IsUsingNativeUI);

            authenticator.Completed += async(s, ee) =>
            {
                if (ee.IsAuthenticated)
                {
                    var user = await oAuth2.GetUserInfoAsync(ee.Account);

                    string name  = user.Name;
                    string email = user.email;
                    string Id    = user.Id;

                    RegisterResultCheck(name, email, "facebook_" + Id);

                    //await Navigation.PushAsync(new TabPage());

                    Debug.WriteLine("Authentication Success");
                }
            };
            authenticator.Error += (s, ee) =>
            {
                Debug.WriteLine("Authentication error: " + ee.Message);
            };

            var presenter = new Xamarin.Auth.Presenters.OAuthLoginPresenter();

            presenter.Login(authenticator);
        }
        public Task LoginWithSNSAsync(SNSProvider provider)
        {
            try
            {
                oAuth2 = OAuth2ProviderFactory.CreateProvider(provider);
                var authenticator = new OAuth2Authenticator(
                    oAuth2.ClientId,
                    oAuth2.ClientSecret,
                    oAuth2.Scope,
                    oAuth2.AuthorizationUri,
                    oAuth2.RedirectUri,
                    oAuth2.RequestTokenUri,
                    null,
                    oAuth2.IsUsingNativeUI);

                authenticator.Completed += async(s, e) =>
                {
                    if (e.IsAuthenticated)
                    {
                        var user = await oAuth2.GetUserInfoAsync(e.Account);

                        AppSettings.User = user;
                        MessagingCenter.Send(user, MessengerKey.AuthenticationRequested, true);
                        Debug.WriteLine("Authentication Success");
                    }
                };
                authenticator.Error += (s, e) =>
                {
                    Debug.WriteLine("Authentication error: " + e.Message);
                };

                var presenter = new Xamarin.Auth.Presenters.OAuthLoginPresenter();
                presenter.Login(authenticator);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Login Error : " + ex.Message);
                return(Task.FromResult(false));
            }
            return(Task.FromResult(true));
        }