diff options
author | Andrew Arnott <andrewarnott@gmail.com> | 2013-01-23 21:50:40 -0800 |
---|---|---|
committer | Andrew Arnott <andrewarnott@gmail.com> | 2013-01-23 21:50:40 -0800 |
commit | 98976665f90b20e4757e932ce3a33268f7e1daa6 (patch) | |
tree | eeefb570e357a608dbe8a20ab1a033233cc65a7a /samples/OAuthConsumerWpf/MainWindow.xaml.cs | |
parent | b16591e45dd70602b1899d760200e72fd6d5eaec (diff) | |
download | DotNetOpenAuth-98976665f90b20e4757e932ce3a33268f7e1daa6.zip DotNetOpenAuth-98976665f90b20e4757e932ce3a33268f7e1daa6.tar.gz DotNetOpenAuth-98976665f90b20e4757e932ce3a33268f7e1daa6.tar.bz2 |
Fixed a bunch more samples.
Diffstat (limited to 'samples/OAuthConsumerWpf/MainWindow.xaml.cs')
-rw-r--r-- | samples/OAuthConsumerWpf/MainWindow.xaml.cs | 55 |
1 files changed, 29 insertions, 26 deletions
diff --git a/samples/OAuthConsumerWpf/MainWindow.xaml.cs b/samples/OAuthConsumerWpf/MainWindow.xaml.cs index 310c2c6..07e127b 100644 --- a/samples/OAuthConsumerWpf/MainWindow.xaml.cs +++ b/samples/OAuthConsumerWpf/MainWindow.xaml.cs @@ -6,21 +6,21 @@ using System.IO; using System.Linq; using System.Net; + using System.Net.Http; using System.Security.Cryptography.X509Certificates; using System.ServiceModel; using System.ServiceModel.Channels; + using System.Threading; + using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Xml.Linq; - using DotNetOpenAuth.ApplicationBlock; using DotNetOpenAuth.Messaging; using DotNetOpenAuth.OAuth; using DotNetOpenAuth.OAuth.ChannelElements; using DotNetOpenAuth.Samples.OAuthConsumerWpf.WcfSampleService; - using OAuth2; - using OAuth2 = DotNetOpenAuth.OAuth2; using ProtocolVersion = DotNetOpenAuth.OAuth.ProtocolVersion; @@ -64,7 +64,7 @@ this.wcf = new UserAgentClient(authServer, "sampleconsumer", "samplesecret"); } - private void beginAuthorizationButton_Click(object sender, RoutedEventArgs e) { + private async void beginAuthorizationButton_Click(object sender, RoutedEventArgs e) { if (string.IsNullOrEmpty(this.googleTokenManager.ConsumerKey)) { MessageBox.Show(this, "You must modify the App.config or OAuthConsumerWpf.exe.config file for this application to include your Google OAuth consumer key first.", "Configuration required", MessageBoxButton.OK, MessageBoxImage.Stop); return; @@ -72,17 +72,16 @@ var auth = new Authorize( this.google, - (DesktopConsumer consumer, out string requestToken) => - GoogleConsumer.RequestAuthorization( + consumer => + GoogleConsumer.RequestAuthorizationAsync( consumer, - GoogleConsumer.Applications.Contacts | GoogleConsumer.Applications.Blogger, - out requestToken)); + GoogleConsumer.Applications.Contacts | GoogleConsumer.Applications.Blogger)); bool? result = auth.ShowDialog(); if (result.HasValue && result.Value) { this.googleAccessToken = auth.AccessToken; this.postButton.IsEnabled = true; - XDocument contactsDocument = GoogleConsumer.GetContacts(this.google, this.googleAccessToken, 25, 1); + XDocument contactsDocument = await GoogleConsumer.GetContactsAsync(this.google, this.googleAccessToken); var contacts = from entry in contactsDocument.Root.Elements(XName.Get("entry", "http://www.w3.org/2005/Atom")) select new { Name = entry.Element(XName.Get("title", "http://www.w3.org/2005/Atom")).Value, Email = entry.Element(XName.Get("email", "http://schemas.google.com/g/2005")).Attribute("address").Value }; this.contactsGrid.Children.Clear(); @@ -99,12 +98,12 @@ } } - private void postButton_Click(object sender, RoutedEventArgs e) { + private async void postButton_Click(object sender, RoutedEventArgs e) { XElement postBodyXml = XElement.Parse(this.postBodyBox.Text); - GoogleConsumer.PostBlogEntry(this.google, this.googleAccessToken, this.blogUrlBox.Text, this.postTitleBox.Text, postBodyXml); + await GoogleConsumer.PostBlogEntryAsync(this.google, this.googleAccessToken, this.blogUrlBox.Text, this.postTitleBox.Text, postBodyXml, CancellationToken.None); } - private void beginWcfAuthorizationButton_Click(object sender, RoutedEventArgs e) { + private async void beginWcfAuthorizationButton_Click(object sender, RoutedEventArgs e) { var auth = new Authorize2(this.wcf); auth.Authorization.Scope.AddRange(OAuthUtilities.SplitScopes("http://tempuri.org/IDataApi/GetName http://tempuri.org/IDataApi/GetAge http://tempuri.org/IDataApi/GetFavoriteSites")); auth.Authorization.Callback = new Uri("http://localhost:59721/"); @@ -112,20 +111,20 @@ bool? result = auth.ShowDialog(); if (result.HasValue && result.Value) { this.wcfAccessToken = auth.Authorization; - this.wcfName.Content = this.CallService(client => client.GetName()); - this.wcfAge.Content = this.CallService(client => client.GetAge()); - this.wcfFavoriteSites.Content = this.CallService(client => string.Join(", ", client.GetFavoriteSites())); + this.wcfName.Content = await this.CallServiceAsync(client => client.GetName()); + this.wcfAge.Content = await this.CallServiceAsync(client => client.GetAge()); + this.wcfFavoriteSites.Content = await this.CallServiceAsync(client => string.Join(", ", client.GetFavoriteSites())); } } - private T CallService<T>(Func<DataApiClient, T> predicate) { + private async Task<T> CallServiceAsync<T>(Func<DataApiClient, T> predicate) { DataApiClient client = new DataApiClient(); if (this.wcfAccessToken == null) { throw new InvalidOperationException("No access token!"); } var httpRequest = (HttpWebRequest)WebRequest.Create(client.Endpoint.Address.Uri); - this.wcf.AuthorizeRequest(httpRequest, this.wcfAccessToken); + await this.wcf.AuthorizeRequestAsync(httpRequest, this.wcfAccessToken, CancellationToken.None); HttpRequestMessageProperty httpDetails = new HttpRequestMessageProperty(); httpDetails.Headers[HttpRequestHeader.Authorization] = httpRequest.Headers[HttpRequestHeader.Authorization]; @@ -135,7 +134,7 @@ } } - private void beginButton_Click(object sender, RoutedEventArgs e) { + private async void beginButton_Click(object sender, RoutedEventArgs e) { try { var service = new ServiceProviderDescription { RequestTokenEndpoint = new MessageReceivingEndpoint(this.requestTokenUrlBox.Text, this.requestTokenHttpMethod.SelectedIndex == 0 ? HttpDeliveryMethods.GetRequest : HttpDeliveryMethods.PostRequest), @@ -151,16 +150,17 @@ var consumer = new DesktopConsumer(service, tokenManager); string accessToken; if (service.ProtocolVersion == ProtocolVersion.V10) { - string requestToken; - Uri authorizeUrl = consumer.RequestUserAuthorization(null, null, out requestToken); + var tuple = await consumer.RequestUserAuthorizationAsync(null, null); + Uri authorizeUrl = tuple.Item1; + string requestToken = tuple.Item2; Process.Start(authorizeUrl.AbsoluteUri); MessageBox.Show(this, "Click OK when you've authorized the app."); - var authorizationResponse = consumer.ProcessUserAuthorization(requestToken, null); + var authorizationResponse = await consumer.ProcessUserAuthorizationAsync(requestToken, null); accessToken = authorizationResponse.AccessToken; } else { var authorizePopup = new Authorize( consumer, - (DesktopConsumer c, out string requestToken) => c.RequestUserAuthorization(null, null, out requestToken)); + c => c.RequestUserAuthorizationAsync(null, null)); authorizePopup.Owner = this; bool? result = authorizePopup.ShowDialog(); if (result.HasValue && result.Value) { @@ -174,15 +174,18 @@ resourceHttpMethod |= HttpDeliveryMethods.AuthorizationHeaderRequest; } var resourceEndpoint = new MessageReceivingEndpoint(this.resourceUrlBox.Text, resourceHttpMethod); - using (IncomingWebResponse resourceResponse = consumer.PrepareAuthorizedRequestAndSend(resourceEndpoint, accessToken)) { - this.resultsBox.Text = resourceResponse.GetResponseReader().ReadToEnd(); + var request = await consumer.PrepareAuthorizedRequestAsync(resourceEndpoint, accessToken); + using (var httpClient = new HttpClient()) { + using (var resourceResponse = await httpClient.SendAsync(request)) { + this.resultsBox.Text = await resourceResponse.Content.ReadAsStringAsync(); + } } } catch (DotNetOpenAuth.Messaging.ProtocolException ex) { MessageBox.Show(this, ex.Message); } } - private void oauth2BeginButton_Click(object sender, RoutedEventArgs e) { + private async void oauth2BeginButton_Click(object sender, RoutedEventArgs e) { var authServer = new DotNetOpenAuth.OAuth2.AuthorizationServerDescription { AuthorizationEndpoint = new Uri(this.oauth2AuthorizationUrlBox.Text), }; @@ -206,7 +209,7 @@ var request = (HttpWebRequest)WebRequest.Create(requestUri.Uri); request.Method = this.oauth2ResourceHttpMethodList.SelectedIndex < 2 ? "GET" : "POST"; if (this.oauth2ResourceHttpMethodList.SelectedIndex == 0) { - client.AuthorizeRequest(request, authorizePopup.Authorization); + await client.AuthorizeRequestAsync(request, authorizePopup.Authorization, CancellationToken.None); } using (var resourceResponse = request.GetResponse()) { |