diff options
Diffstat (limited to 'samples/DotNetOpenAuth.ApplicationBlock')
3 files changed, 67 insertions, 19 deletions
diff --git a/samples/DotNetOpenAuth.ApplicationBlock/DotNetOpenAuth.ApplicationBlock.csproj b/samples/DotNetOpenAuth.ApplicationBlock/DotNetOpenAuth.ApplicationBlock.csproj index 590b96b..626dbaa 100644 --- a/samples/DotNetOpenAuth.ApplicationBlock/DotNetOpenAuth.ApplicationBlock.csproj +++ b/samples/DotNetOpenAuth.ApplicationBlock/DotNetOpenAuth.ApplicationBlock.csproj @@ -57,6 +57,7 @@ <ItemGroup> <Compile Include="GoogleConsumer.cs" /> <Compile Include="Properties\AssemblyInfo.cs" /> + <Compile Include="TwitterConsumer.cs" /> <Compile Include="Util.cs" /> </ItemGroup> <ItemGroup> diff --git a/samples/DotNetOpenAuth.ApplicationBlock/GoogleConsumer.cs b/samples/DotNetOpenAuth.ApplicationBlock/GoogleConsumer.cs index c6f2b89..f0a4c03 100644 --- a/samples/DotNetOpenAuth.ApplicationBlock/GoogleConsumer.cs +++ b/samples/DotNetOpenAuth.ApplicationBlock/GoogleConsumer.cs @@ -11,6 +11,7 @@ namespace DotNetOpenAuth.ApplicationBlock { using System.IO; using System.Linq; using System.Net; + using System.Security.Cryptography.X509Certificates; using System.Text; using System.Text.RegularExpressions; using System.Xml; @@ -26,7 +27,7 @@ namespace DotNetOpenAuth.ApplicationBlock { /// <summary> /// The Consumer to use for accessing Google data APIs. /// </summary> - private static readonly ServiceProviderDescription GoogleDescription = new ServiceProviderDescription { + public static readonly ServiceProviderDescription ServiceDescription = 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), @@ -69,26 +70,21 @@ namespace DotNetOpenAuth.ApplicationBlock { } /// <summary> - /// Initializes a new instance of the <see cref="WebConsumer"/> class that is prepared to communicate with Google. + /// The service description to use for accessing Google data APIs using an X509 certificate. /// </summary> - /// <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) { - return new WebConsumer(GoogleDescription, tokenManager) { - ConsumerKey = consumerKey, - }; - } + /// <param name="signingCertificate">The signing certificate.</param> + /// <returns>A service description that can be used to create an instance of + /// <see cref="DesktopConsumer"/> or <see cref="WebConsumer"/>. </returns> + public static ServiceProviderDescription CreateRsaSha1ServiceDescription(X509Certificate2 signingCertificate) { + if (signingCertificate == null) { + throw new ArgumentNullException("signingCertificate"); + } - /// <summary> - /// Initializes a new instance of the <see cref="DesktopConsumer"/> class that is prepared to communicate with Google. - /// </summary> - /// <param name="tokenManager">The token manager.</param> - /// <param name="consumerKey">The consumer key.</param> - /// <returns>The newly instantiated <see cref="DesktopConsumer"/>.</returns> - public static DesktopConsumer CreateDesktopConsumer(ITokenManager tokenManager, string consumerKey) { - return new DesktopConsumer(GoogleDescription, tokenManager) { - ConsumerKey = consumerKey, + return 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 RsaSha1SigningBindingElement(signingCertificate) }, }; } diff --git a/samples/DotNetOpenAuth.ApplicationBlock/TwitterConsumer.cs b/samples/DotNetOpenAuth.ApplicationBlock/TwitterConsumer.cs new file mode 100644 index 0000000..2a98ffe --- /dev/null +++ b/samples/DotNetOpenAuth.ApplicationBlock/TwitterConsumer.cs @@ -0,0 +1,51 @@ +//----------------------------------------------------------------------- +// <copyright file="TwitterConsumer.cs" company="Andrew Arnott"> +// Copyright (c) Andrew Arnott. All rights reserved. +// </copyright> +//----------------------------------------------------------------------- + +namespace DotNetOpenAuth.ApplicationBlock { + using System; + using System.Collections.Generic; + using System.IO; + using System.Xml; + using System.Xml.Linq; + using DotNetOpenAuth.Messaging; + using DotNetOpenAuth.OAuth; + using DotNetOpenAuth.OAuth.ChannelElements; + + /// <summary> + /// A consumer capable of communicating with Twitter. + /// </summary> + public static class TwitterConsumer { + /// <summary> + /// The description of Twitter's OAuth protocol URIs. + /// </summary> + public static readonly ServiceProviderDescription ServiceDescription = new ServiceProviderDescription { + RequestTokenEndpoint = new MessageReceivingEndpoint("http://twitter.com/oauth/request_token", HttpDeliveryMethods.GetRequest | HttpDeliveryMethods.AuthorizationHeaderRequest), + UserAuthorizationEndpoint = new MessageReceivingEndpoint("http://twitter.com/oauth/authorize", HttpDeliveryMethods.GetRequest | HttpDeliveryMethods.AuthorizationHeaderRequest), + AccessTokenEndpoint = new MessageReceivingEndpoint("http://twitter.com/oauth/access_token", HttpDeliveryMethods.GetRequest | HttpDeliveryMethods.AuthorizationHeaderRequest), + TamperProtectionElements = new ITamperProtectionChannelBindingElement[] { new HmacSha1SigningBindingElement() }, + }; + + /// <summary> + /// The URI to get a user's favorites. + /// </summary> + private static readonly MessageReceivingEndpoint GetFavoritesEndpoint = new MessageReceivingEndpoint("http://twitter.com/favorites.xml", HttpDeliveryMethods.GetRequest); + + /// <summary> + /// The URI to get the data on the user's home page. + /// </summary> + private static readonly MessageReceivingEndpoint GetFriendTimelineStatusEndpoint = new MessageReceivingEndpoint("http://twitter.com/statuses/friends_timeline.xml", HttpDeliveryMethods.GetRequest); + + public static XDocument GetUpdates(ConsumerBase twitter, string accessToken) { + IncomingWebResponse response = twitter.PrepareAuthorizedRequestAndSend(GetFriendTimelineStatusEndpoint, accessToken); + return XDocument.Load(XmlReader.Create(response.GetResponseReader())); + } + + public static XDocument GetFavorites(ConsumerBase twitter, string accessToken) { + IncomingWebResponse response = twitter.PrepareAuthorizedRequestAndSend(GetFavoritesEndpoint, accessToken); + return XDocument.Load(XmlReader.Create(response.GetResponseReader())); + } + } +} |