示例#1
0
        void LoginToFacebook(bool allowCancel)
        {
            IOAuthClient.IOAuthClient auth = new PlatformOAuthClient();

            auth.New(dialog,
                     "temporaryKey",
                     clientId: "App ID from https://developers.facebook.com/apps",
                     scope: "",
                     authorizeUrl: new Uri("https://m.facebook.com/dialog/oauth/"),
                     redirectUrl: new Uri("http://www.facebook.com/connect/login_success.html"));

            auth.AllowCancel = allowCancel;

            // If authorization succeeds or is canceled, .Completed will be fired.
            auth.Completed += (s, e) =>
            {
                // We presented the UI, so it's up to us to dismiss it.
                dialog.DismissViewController(true, null);

                if (!e.IsAuthenticated)
                {
                    facebookStatus.Caption = "Not authorized";
                    dialog.ReloadData();
                    return;
                }

                // Now that we're logged in, make a OAuth2 request to get the user's info.
                var request = auth.CreateRequest("GET", new Uri("https://graph.facebook.com/me"), null, e.Account);
                request.GetResponseAsync().ContinueWith(t =>
                {
                    if (t.IsFaulted)
                    {
                        facebookStatus.Caption = "Error: " + t.Exception.InnerException.Message;
                    }
                    else if (t.IsCanceled)
                    {
                        facebookStatus.Caption = "Canceled";
                    }
                    else
                    {
                        var obj = JsonValue.Parse(t.Result.GetResponseText());
                        facebookStatus.Caption = "Logged in as " + obj["name"];
                    }

                    dialog.ReloadData();
                }, uiScheduler);
            };

            auth.Start("Facebook login");
        }
        void Login(bool allowCancel)
        {
            IOAuthClient auth = new PlatformOAuthClient();

            auth.New(this,
                     "temporaryKey",
                     "<client_id>",
                     "<client_secret>",
                     "",
                     new Uri("<authorization_uri>"),
                     new Uri("<redirect_uri>"),
                     new Uri("<token_uri>"));


            auth.AllowCancel = true;

            auth.Error += (s, ee) =>
            {
                var builder = new AlertDialog.Builder(this);
                builder.SetMessage(ee.Message);
                builder.SetPositiveButton("Ok", (o, e) => { });
                builder.Create().Show();
                return;
            };

            // If authorization succeeds or is canceled, .Completed will be fired.
            auth.Completed += (s, ee) =>
            {
                if (!ee.IsAuthenticated)
                {
                    var builder = new AlertDialog.Builder(this);
                    builder.SetMessage("Not Authenticated");
                    builder.SetPositiveButton("Ok", (o, e) => { });
                    builder.Create().Show();
                    return;
                }

                else
                {
                    var builder = new AlertDialog.Builder(this);
                    builder.SetMessage("Authenticated, well done");
                    builder.SetPositiveButton("Ok", (o, e) => { });
                    builder.Create().Show();
                    return;
                }
            };

            auth.Start("Xamarin.Auth login");
        }
        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

            IOAuthClient auth = new PlatformOAuthClient();

            auth.New(this,
                     "temporaryKey",
                     clientId: "<ClientID>",
                     scope: "<scope>",
                     authorizeUrl: new Uri("<AuthorizeUrl>"),
                     redirectUrl: new Uri("<RedirectUrl>"));

            auth.AllowCancel = true;

            //auth.Error += (s, ee) =>
            //{
            //    Console.WriteLine(ee.Message);
            //};

            // If authorization succeeds or is canceled, .Completed will be fired.
            auth.Completed += (s, ee) =>
            {
                if (!ee.IsAuthenticated)
                {
                    MessageBox.Show("Not Authenticated");
                    return;
                }

                else
                {
                    MessageBox.Show("Authenticated, well done");
                    return;
                }
            };

            auth.Start("Login");
        }