Esempio n. 1
0
        public void Auth()
        {
            var clientId     = "588035480561-9viuq54cpc427jefm8qh4c9d46guhl4d.apps.googleusercontent.com";
            var scope        = "profile";
            var authorizeUrl = new Uri("https://accounts.google.com/o/oauth2/auth");
            var redirectUrl  = new Uri("http://localhost");

            var auth = new Xamarin.Auth.OAuth2Authenticator(clientId, scope, authorizeUrl, redirectUrl);

            auth.Completed += (sender, eventArgs) => {
                if (eventArgs.IsAuthenticated)
                {
                    var requestAddress = authorizeUrl.AbsolutePath + "?client_id=" + clientId + "&redirect_url=" + redirectUrl.AbsolutePath
                                         + "&response_type=code";
                    var requestUrl = new Uri(requestAddress);
                    var request    = new OAuth2Request("GET", requestUrl, null, eventArgs.Account);
                    request.GetResponseAsync().ContinueWith(t =>
                    {
                        if (t.IsFaulted)
                        {
                            var s = t.Status;
                        }
                        else
                        {
                            var json = t.Result.GetResponseText();
                        }
                    });
                }
                else
                {
                    var s = scope;
                }
            };
            auth.Error += (sender, eventArgs) =>
            {
                var s = sender;
            };
        }
Esempio n. 2
0
        void Facebook_Clicked(object sender, System.EventArgs e)
        {
            authenticator
                = new Xamarin.Auth.OAuth2Authenticator
                  (
                      clientId:
                      new Func <string>
                      (
                          () =>
            {
                string retval_client_id = "oops something is wrong!";

                retval_client_id = fb_app_id;
                return(retval_client_id);
            }
                      ).Invoke(),
                      authorizeUrl:
                      new Func <Uri>
                      (
                          () =>
            {
                string uri = null;
                if (native_ui)
                {
                    uri = "https://www.facebook.com/v2.9/dialog/oauth";
                }
                else
                {
                    // old
                    uri = "https://m.facebook.com/dialog/oauth/";
                }
                return(new Uri(uri));
            }
                      ).Invoke(),
                      redirectUrl:
                      new Func <Uri>
                      (
                          () =>
            {
                string uri = null;
                if (native_ui)
                {
                    uri =
                        $"fb{fb_app_id}://authorize"
                    ;
                }
                else
                {
                    uri =
                        $"fb{fb_app_id}://authorize"
                    ;
                }
                return(new Uri(uri));
            }
                      ).Invoke(),
                      scope: "", // "basic", "email",
                      getUsernameAsync: null,
                      isUsingNativeUI: native_ui
                  )
                {
                AllowCancel = true,
                };

            authenticator.Completed +=
                (s, ea) =>
            {
                StringBuilder sb = new StringBuilder();

                if (ea.Account != null && ea.Account.Properties != null)
                {
                    sb.Append("Token = ").AppendLine($"{ea.Account.Properties["access_token"]}");
                }
                else
                {
                    sb.Append("Not authenticated ").AppendLine($"Account.Properties does not exist");
                }

                Navigation.PushAsync(new DashBoardPage());
                return;
            };

            authenticator.Error +=
                (s, ea) =>
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("Error = ").AppendLine($"{ea.Message}");

                DisplayAlert
                (
                    "Authentication Error",
                    sb.ToString(),
                    "OK"
                );
                return;
            };

            // after initialization (creation and event subscribing) exposing local object
            AuthenticationState.Authenticator = authenticator;

            PresentUILoginScreen(authenticator);

            return;
        }