summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrew Arnott <andrewarnott@gmail.com>2008-11-02 22:51:25 -0800
committerAndrew <andrewarnott@gmail.com>2008-11-02 22:51:25 -0800
commitfe66a05013df51312dec9074eed7915ba3b2da8a (patch)
treef2188cf7f97147f0f92fd306078404325f218b76
parent8969dfec9e4e4ecc45f909137dc3a23d7af0bee8 (diff)
downloadDotNetOpenAuth-fe66a05013df51312dec9074eed7915ba3b2da8a.zip
DotNetOpenAuth-fe66a05013df51312dec9074eed7915ba3b2da8a.tar.gz
DotNetOpenAuth-fe66a05013df51312dec9074eed7915ba3b2da8a.tar.bz2
Applied the Google common consumer class to the WPF sample.
-rw-r--r--samples/ConsumerWpf/Constants.cs48
-rw-r--r--samples/ConsumerWpf/ConsumerWpf.csproj1
-rw-r--r--samples/ConsumerWpf/MainWindow.xaml.cs19
-rw-r--r--src/DotNetOAuth/CommonConsumers/GoogleConsumer.cs46
4 files changed, 43 insertions, 71 deletions
diff --git a/samples/ConsumerWpf/Constants.cs b/samples/ConsumerWpf/Constants.cs
deleted file mode 100644
index cc21908..0000000
--- a/samples/ConsumerWpf/Constants.cs
+++ /dev/null
@@ -1,48 +0,0 @@
-//-----------------------------------------------------------------------
-// <copyright file="Constants.cs" company="Andrew Arnott">
-// Copyright (c) Andrew Arnott. All rights reserved.
-// </copyright>
-//-----------------------------------------------------------------------
-
-namespace DotNetOAuth.Samples.ConsumerWpf {
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using DotNetOAuth;
- using DotNetOAuth.ChannelElements;
- using DotNetOAuth.Messaging;
-
- /// <summary>
- /// Service Provider definitions.
- /// </summary>
- public static class Constants {
- /// <summary>
- /// The Consumer to use for accessing Google data APIs.
- /// </summary>
- public static readonly ServiceProviderDescription GoogleDescription = new ServiceProviderDescription {
- RequestTokenEndpoint = new MessageReceivingEndpoint("https://www.google.com/accounts/OAuthGetRequestToken", HttpDeliveryMethods.AuthorizationHeaderRequest | HttpDeliveryMethods.GetRequest),
- UserAuthorizationEndpoint = new MessageReceivingEndpoint("https://www.google.com/accounts/OAuthAuthorizeToken", HttpDeliveryMethods.AuthorizationHeaderRequest | HttpDeliveryMethods.GetRequest),
- AccessTokenEndpoint = new MessageReceivingEndpoint("https://www.google.com/accounts/OAuthGetAccessToken", HttpDeliveryMethods.AuthorizationHeaderRequest | HttpDeliveryMethods.GetRequest),
- TamperProtectionElements = new ITamperProtectionChannelBindingElement[] {
- new HmacSha1SigningBindingElement(),
- },
- };
-
- /// <summary>
- /// Values of the "scope" parameter that indicates what data streams the Consumer
- /// wants access to.
- /// </summary>
- public static class GoogleScopes {
- /// <summary>
- /// Access to the Gmail address book.
- /// </summary>
- public const string Contacts = "http://www.google.com/m8/feeds/";
-
- /// <summary>
- /// The URI to get contacts once authorization is granted.
- /// </summary>
- public static readonly MessageReceivingEndpoint GetContacts = new MessageReceivingEndpoint("http://www.google.com/m8/feeds/contacts/default/full/", HttpDeliveryMethods.GetRequest);
- }
- }
-} \ No newline at end of file
diff --git a/samples/ConsumerWpf/ConsumerWpf.csproj b/samples/ConsumerWpf/ConsumerWpf.csproj
index 389894f..4632889 100644
--- a/samples/ConsumerWpf/ConsumerWpf.csproj
+++ b/samples/ConsumerWpf/ConsumerWpf.csproj
@@ -69,7 +69,6 @@
</Compile>
</ItemGroup>
<ItemGroup>
- <Compile Include="Constants.cs" />
<Compile Include="InMemoryTokenManager.cs" />
<Compile Include="Properties\AssemblyInfo.cs">
<SubType>Code</SubType>
diff --git a/samples/ConsumerWpf/MainWindow.xaml.cs b/samples/ConsumerWpf/MainWindow.xaml.cs
index e1a4b6b..37dc288 100644
--- a/samples/ConsumerWpf/MainWindow.xaml.cs
+++ b/samples/ConsumerWpf/MainWindow.xaml.cs
@@ -15,6 +15,7 @@
using System.Xml.Linq;
using DotNetOAuth;
using DotNetOAuth.ChannelElements;
+ using DotNetOAuth.CommonConsumers;
using DotNetOAuth.Messaging;
/// <summary>
@@ -28,7 +29,7 @@
public MainWindow() {
InitializeComponent();
- this.google = new DesktopConsumer(Constants.GoogleDescription, this.tokenManager);
+ this.google = GoogleConsumer.CreateDesktopConsumer(this.tokenManager, string.Empty);
}
private void beginAuthorizationButton_Click(object sender, RoutedEventArgs e) {
@@ -36,22 +37,18 @@
this.tokenManager.ConsumerSecret = consumerSecretBox.Text;
this.google.ConsumerKey = consumerKeyBox.Text;
- var extraParameters = new Dictionary<string, string> {
- { "scope", Constants.GoogleScopes.Contacts },
- };
- Uri browserAuthorizationLocation = this.google.RequestUserAuthorization(extraParameters, null, out this.requestToken);
+ Uri browserAuthorizationLocation = GoogleConsumer.RequestAuthorization(this.google, GoogleConsumer.Applications.Contacts, out this.requestToken);
System.Diagnostics.Process.Start(browserAuthorizationLocation.AbsoluteUri);
}
private void completeAuthorizationButton_Click(object sender, RoutedEventArgs e) {
var grantedAccess = this.google.ProcessUserAuthorization(this.requestToken);
- Response contactsResponse = this.google.PrepareAuthorizedRequestAndSend(Constants.GoogleScopes.GetContacts, grantedAccess.AccessToken);
- XDocument contactsDocument = XDocument.Parse(contactsResponse.Body);
+ XDocument contactsDocument = GoogleConsumer.GetContacts(this.google, grantedAccess.AccessToken);
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,
- };
+ 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,
+ };
contactsGrid.Children.Clear();
foreach (var contact in contacts) {
contactsGrid.RowDefinitions.Add(new RowDefinition());
diff --git a/src/DotNetOAuth/CommonConsumers/GoogleConsumer.cs b/src/DotNetOAuth/CommonConsumers/GoogleConsumer.cs
index 5f6bf21..97367aa 100644
--- a/src/DotNetOAuth/CommonConsumers/GoogleConsumer.cs
+++ b/src/DotNetOAuth/CommonConsumers/GoogleConsumer.cs
@@ -10,6 +10,7 @@ namespace DotNetOAuth.CommonConsumers {
using System.Linq;
using System.Xml.Linq;
using DotNetOAuth.ChannelElements;
+ using DotNetOAuth.Messages;
using DotNetOAuth.Messaging;
/// <summary>
@@ -61,8 +62,7 @@ namespace DotNetOAuth.CommonConsumers {
/// <param name="tokenManager">The token manager.</param>
/// <param name="consumerKey">The consumer key.</param>
/// <returns>The newly instantiated <see cref="WebConsumer"/>.</returns>
- public static WebConsumer CreateWebConsumer(ITokenManager tokenManager, string consumerKey)
- {
+ public static WebConsumer CreateWebConsumer(ITokenManager tokenManager, string consumerKey) {
return new WebConsumer(GoogleDescription, tokenManager) {
ConsumerKey = consumerKey,
};
@@ -85,21 +85,26 @@ namespace DotNetOAuth.CommonConsumers {
/// </summary>
/// <param name="consumer">The Google consumer previously constructed using <see cref="CreateWebConsumer"/> or <see cref="CreateDesktopConsumer"/>.</param>
/// <param name="requestedAccessScope">The requested access scope.</param>
- public static void RequestAuthorization(ConsumerBase consumer, Applications requestedAccessScope) {
- if (consumer == null) {
- throw new ArgumentNullException("consumer");
- }
-
+ public static void RequestAuthorization(WebConsumer consumer, Applications requestedAccessScope) {
Uri callback = MessagingUtilities.GetRequestUrlFromContext().StripQueryArgumentsWithPrefix(Protocol.Default.ParameterPrefix);
- var extraParameters = new Dictionary<string, string> {
- { "scope", GetScopeUri(requestedAccessScope) },
- };
string requestToken;
- var request = consumer.PrepareRequestUserAuthorization(callback, extraParameters, null, out requestToken);
+ var request = RequestAuthorizationInternal(consumer, callback, requestedAccessScope, out requestToken);
consumer.Channel.Send(request).Send();
}
/// <summary>
+ /// Requests authorization from Google to access data from a set of Google applications.
+ /// </summary>
+ /// <param name="consumer">The Google consumer previously constructed using <see cref="CreateWebConsumer"/> or <see cref="CreateDesktopConsumer"/>.</param>
+ /// <param name="requestedAccessScope">The requested access scope.</param>
+ /// <param name="requestToken">The unauthorized request token assigned by Google.</param>
+ /// <returns>The request token</returns>
+ public static Uri RequestAuthorization(DesktopConsumer consumer, Applications requestedAccessScope, out string requestToken) {
+ var request = RequestAuthorizationInternal(consumer, null, requestedAccessScope, out requestToken);
+ return consumer.Channel.Send(request).DirectUriRequest;
+ }
+
+ /// <summary>
/// Gets the Gmail address book's contents.
/// </summary>
/// <param name="consumer">The Google consumer previously constructed using <see cref="CreateWebConsumer"/> or <see cref="CreateDesktopConsumer"/>.</param>
@@ -123,5 +128,24 @@ namespace DotNetOAuth.CommonConsumers {
private static string GetScopeUri(Applications scope) {
return string.Join(" ", Util.GetIndividualFlags(scope).Select(app => DataScopeUris[(Applications)app]).ToArray());
}
+
+ /// <summary>
+ /// Requests authorization from Google to access data from a set of Google applications.
+ /// </summary>
+ /// <param name="consumer">The Google consumer previously constructed using <see cref="CreateWebConsumer"/> or <see cref="CreateDesktopConsumer"/>.</param>
+ /// <param name="callback">The callback URI, if in web mode.</param>
+ /// <param name="requestedAccessScope">The requested access scope.</param>
+ /// <param name="requestToken">The unauthorized request token assigned by Google.</param>
+ /// <returns>The request token</returns>
+ private static UserAuthorizationRequest RequestAuthorizationInternal(ConsumerBase consumer, Uri callback, Applications requestedAccessScope, out string requestToken) {
+ if (consumer == null) {
+ throw new ArgumentNullException("consumer");
+ }
+
+ var extraParameters = new Dictionary<string, string> {
+ { "scope", GetScopeUri(requestedAccessScope) },
+ };
+ return consumer.PrepareRequestUserAuthorization(callback, extraParameters, null, out requestToken);
+ }
}
}