44// </copyright>
55//-----------------------------------------------------------------------
66
7- namespace DotNetOpenAuth . ApplicationBlock
8- {
7+ namespace DotNetOpenAuth . ApplicationBlock {
98 using System ;
109 using System . Collections . Generic ;
1110 using System . Diagnostics ;
@@ -29,17 +28,15 @@ namespace DotNetOpenAuth.ApplicationBlock
2928 /// <summary>
3029 /// A consumer capable of communicating with Google Data APIs.
3130 /// </summary>
32- public static class GoogleConsumer
33- {
31+ public class GoogleConsumer : Consumer {
3432 /// <summary>
3533 /// The Consumer to use for accessing Google data APIs.
3634 /// </summary>
37- public static readonly ServiceProviderDescription ServiceDescription = new ServiceProviderDescription {
38- RequestTokenEndpoint = new MessageReceivingEndpoint ( "https://www.google.com/accounts/OAuthGetRequestToken" , HttpDeliveryMethods . AuthorizationHeaderRequest | HttpDeliveryMethods . GetRequest ) ,
39- UserAuthorizationEndpoint = new MessageReceivingEndpoint ( "https://www.google.com/accounts/OAuthAuthorizeToken" , HttpDeliveryMethods . AuthorizationHeaderRequest | HttpDeliveryMethods . GetRequest ) ,
40- AccessTokenEndpoint = new MessageReceivingEndpoint ( "https://www.google.com/accounts/OAuthGetAccessToken" , HttpDeliveryMethods . AuthorizationHeaderRequest | HttpDeliveryMethods . GetRequest ) ,
41- TamperProtectionElements = new ITamperProtectionChannelBindingElement [ ] { new HmacSha1SigningBindingElement ( ) } ,
42- } ;
35+ public static readonly ServiceProviderDescription ServiceDescription =
36+ new ServiceProviderDescription (
37+ "https://www.google.com/accounts/OAuthGetRequestToken" ,
38+ "https://www.google.com/accounts/OAuthAuthorizeToken" ,
39+ "https://www.google.com/accounts/OAuthGetAccessToken" ) ;
4340
4441 /// <summary>
4542 /// A mapping between Google's applications and their URI scope values.
@@ -72,8 +69,7 @@ public static class GoogleConsumer
7269 /// The many specific authorization scopes Google offers.
7370 /// </summary>
7471 [ Flags ]
75- public enum Applications : long
76- {
72+ public enum Applications : long {
7773 /// <summary>
7874 /// The Gmail address book.
7975 /// </summary>
@@ -155,23 +151,7 @@ public enum Applications : long
155151 Maps = 0x8000 ,
156152 }
157153
158- /// <summary>
159- /// The service description to use for accessing Google data APIs using an X509 certificate.
160- /// </summary>
161- /// <param name="signingCertificate">The signing certificate.</param>
162- /// <returns>A service description that can be used to create an instance of
163- /// <see cref="DesktopConsumer"/> or <see cref="WebConsumer"/>. </returns>
164- public static ServiceProviderDescription CreateRsaSha1ServiceDescription ( X509Certificate2 signingCertificate ) {
165- if ( signingCertificate == null ) {
166- throw new ArgumentNullException ( "signingCertificate" ) ;
167- }
168-
169- return new ServiceProviderDescription {
170- RequestTokenEndpoint = new MessageReceivingEndpoint ( "https://www.google.com/accounts/OAuthGetRequestToken" , HttpDeliveryMethods . AuthorizationHeaderRequest | HttpDeliveryMethods . GetRequest ) ,
171- UserAuthorizationEndpoint = new MessageReceivingEndpoint ( "https://www.google.com/accounts/OAuthAuthorizeToken" , HttpDeliveryMethods . AuthorizationHeaderRequest | HttpDeliveryMethods . GetRequest ) ,
172- AccessTokenEndpoint = new MessageReceivingEndpoint ( "https://www.google.com/accounts/OAuthGetAccessToken" , HttpDeliveryMethods . AuthorizationHeaderRequest | HttpDeliveryMethods . GetRequest ) ,
173- TamperProtectionElements = new ITamperProtectionChannelBindingElement [ ] { new RsaSha1ConsumerSigningBindingElement ( signingCertificate ) } ,
174- } ;
154+ public GoogleConsumer ( ) {
175155 }
176156
177157 /// <summary>
@@ -184,40 +164,12 @@ public static ServiceProviderDescription CreateRsaSha1ServiceDescription(X509Cer
184164 /// A task that completes with the asynchronous operation.
185165 /// </returns>
186166 /// <exception cref="System.ArgumentNullException">consumer</exception>
187- public static async Task RequestAuthorizationAsync ( WebConsumer consumer , Applications requestedAccessScope , CancellationToken cancellationToken = default ( CancellationToken ) ) {
188- if ( consumer == null ) {
189- throw new ArgumentNullException ( "consumer" ) ;
190- }
191-
167+ public Task < Uri > RequestUserAuthorizationAsync ( Applications requestedAccessScope , CancellationToken cancellationToken = default ( CancellationToken ) ) {
192168 var extraParameters = new Dictionary < string , string > {
193169 { "scope" , GetScopeUri ( requestedAccessScope ) } ,
194170 } ;
195171 Uri callback = Util . GetCallbackUrlFromContext ( ) ;
196- var request = await consumer . PrepareRequestUserAuthorizationAsync ( callback , extraParameters , null , cancellationToken ) ;
197- var redirectingResponse = await consumer . Channel . PrepareResponseAsync ( request , cancellationToken ) ;
198- redirectingResponse . Send ( ) ;
199- }
200-
201- /// <summary>
202- /// Requests authorization from Google to access data from a set of Google applications.
203- /// </summary>
204- /// <param name="consumer">The Google consumer previously constructed using <see cref="CreateWebConsumer" /> or <see cref="CreateDesktopConsumer" />.</param>
205- /// <param name="requestedAccessScope">The requested access scope.</param>
206- /// <param name="cancellationToken">The cancellation token.</param>
207- /// <returns>
208- /// The URI to redirect to and the request token.
209- /// </returns>
210- /// <exception cref="System.ArgumentNullException">consumer</exception>
211- public static Task < Tuple < Uri , string > > RequestAuthorizationAsync ( DesktopConsumer consumer , Applications requestedAccessScope , CancellationToken cancellationToken = default ( CancellationToken ) ) {
212- if ( consumer == null ) {
213- throw new ArgumentNullException ( "consumer" ) ;
214- }
215-
216- var extraParameters = new Dictionary < string , string > {
217- { "scope" , GetScopeUri ( requestedAccessScope ) } ,
218- } ;
219-
220- return consumer . RequestUserAuthorizationAsync ( extraParameters , null , cancellationToken ) ;
172+ return this . RequestUserAuthorizationAsync ( callback , extraParameters , cancellationToken ) ;
221173 }
222174
223175 /// <summary>
@@ -232,14 +184,10 @@ public static ServiceProviderDescription CreateRsaSha1ServiceDescription(X509Cer
232184 /// An XML document returned by Google.
233185 /// </returns>
234186 /// <exception cref="System.ArgumentNullException">consumer</exception>
235- public static async Task < XDocument > GetContactsAsync ( ConsumerBase consumer , string accessToken , int maxResults = 25 , int startIndex = 1 , CancellationToken cancellationToken = default ( CancellationToken ) ) {
236- if ( consumer == null ) {
237- throw new ArgumentNullException ( "consumer" ) ;
238- }
239-
187+ public async Task < XDocument > GetContactsAsync ( AccessToken accessToken , int maxResults = 25 , int startIndex = 1 , CancellationToken cancellationToken = default ( CancellationToken ) ) {
240188 // Enable gzip compression. Google only compresses the response for recognized user agent headers. - Mike Lim
241189 var handler = new HttpClientHandler { AutomaticDecompression = DecompressionMethods . GZip } ;
242- using ( var httpClient = consumer . CreateHttpClient ( accessToken , handler ) ) {
190+ using ( var httpClient = this . CreateHttpClient ( accessToken , handler ) ) {
243191 var request = new HttpRequestMessage ( HttpMethod . Get , GetContactsEndpoint ) ;
244192 request . Content = new FormUrlEncodedContent (
245193 new Dictionary < string , string > ( ) {
@@ -255,7 +203,7 @@ public static ServiceProviderDescription CreateRsaSha1ServiceDescription(X509Cer
255203 }
256204 }
257205
258- public static async Task PostBlogEntryAsync ( ConsumerBase consumer , string accessToken , string blogUrl , string title , XElement body , CancellationToken cancellationToken ) {
206+ public async Task PostBlogEntryAsync ( AccessToken accessToken , string blogUrl , string title , XElement body , CancellationToken cancellationToken ) {
259207 string feedUrl ;
260208 var getBlogHome = WebRequest . Create ( blogUrl ) ;
261209 using ( var blogHomeResponse = getBlogHome . GetResponse ( ) ) {
@@ -285,7 +233,7 @@ public static async Task PostBlogEntryAsync(ConsumerBase consumer, string access
285233 var request = new HttpRequestMessage ( HttpMethod . Post , feedUrl ) ;
286234 request . Content = new StreamContent ( ms ) ;
287235 request . Content . Headers . ContentType = new MediaTypeHeaderValue ( "application/atom+xml" ) ;
288- using ( var httpClient = consumer . CreateHttpClient ( accessToken ) ) {
236+ using ( var httpClient = this . CreateHttpClient ( accessToken ) ) {
289237 using ( var response = await httpClient . SendAsync ( request , cancellationToken ) ) {
290238 if ( response . StatusCode == HttpStatusCode . Created ) {
291239 // Success
0 commit comments