1- //-----------------------------------------------------------------------------
1+ //-----------------------------------------------------------------------------
22// <copyright file="DropboxOAuth2Helper.cs" company="Dropbox Inc">
33// Copyright (c) Dropbox Inc. All rights reserved.
44// </copyright>
77namespace Dropbox . Api
88{
99 using System ;
10- using System . Collections . Generic ;
11- using System . Net . Http ;
12- using System . Text ;
10+ using System . Collections . Generic ;
11+ using System . Net . Http ;
12+ using System . Text ;
1313 using System . Threading . Tasks ;
1414
1515 /// <summary>
@@ -210,8 +210,6 @@ public static Uri GetAuthorizeUri(OAuthResponseType oauthResponseType, string cl
210210 throw new ArgumentNullException ( "redirectUri" ) ;
211211 }
212212
213- var dict = new Dictionary < string , string > ( ) ;
214-
215213 var queryBuilder = new StringBuilder ( ) ;
216214
217215 queryBuilder . Append ( "response_type=" ) ;
@@ -249,8 +247,10 @@ public static Uri GetAuthorizeUri(OAuthResponseType oauthResponseType, string cl
249247 queryBuilder . Append ( "&disable_signup=true" ) ;
250248 }
251249
252- var uriBuilder = new UriBuilder ( "https://www.dropbox.com/1/oauth2/authorize" ) ;
253- uriBuilder . Query = queryBuilder . ToString ( ) ;
250+ var uriBuilder = new UriBuilder ( "https://www.dropbox.com/1/oauth2/authorize" )
251+ {
252+ Query = queryBuilder . ToString ( )
253+ } ;
254254
255255 return uriBuilder . Uri ;
256256 }
@@ -271,15 +271,15 @@ public static OAuth2Response ParseTokenFragment(Uri redirectedUri)
271271 var fragment = redirectedUri . Fragment ;
272272 if ( string . IsNullOrWhiteSpace ( fragment ) )
273273 {
274- throw new ArgumentException ( "redirectedUri" , " The supplied uri doesn't contain a fragment") ;
274+ throw new ArgumentException ( "The supplied uri doesn't contain a fragment" , "redirectedUri ") ;
275275 }
276276
277277 fragment = fragment . TrimStart ( '#' ) ;
278278
279- string access_token = null ;
279+ string accessToken = null ;
280280 string uid = null ;
281281 string state = null ;
282- string token_type = null ;
282+ string tokenType = null ;
283283
284284 foreach ( var pair in fragment . Split ( '&' ) )
285285 {
@@ -289,11 +289,10 @@ public static OAuth2Response ParseTokenFragment(Uri redirectedUri)
289289 continue ;
290290 }
291291
292-
293292 switch ( elements [ 0 ] )
294293 {
295294 case "access_token" :
296- access_token = Uri . UnescapeDataString ( elements [ 1 ] ) ;
295+ accessToken = Uri . UnescapeDataString ( elements [ 1 ] ) ;
297296 break ;
298297 case "uid" :
299298 uid = Uri . UnescapeDataString ( elements [ 1 ] ) ;
@@ -302,14 +301,14 @@ public static OAuth2Response ParseTokenFragment(Uri redirectedUri)
302301 state = Uri . UnescapeDataString ( elements [ 1 ] ) ;
303302 break ;
304303 case "token_type" :
305- token_type = Uri . UnescapeDataString ( elements [ 1 ] ) ;
304+ tokenType = Uri . UnescapeDataString ( elements [ 1 ] ) ;
306305 break ;
307306 default :
308- throw new ArgumentException ( "redirectedUri" , " Unexpected values in fragment") ;
307+ throw new ArgumentException ( "Unexpected values in fragment" , "redirectedUri ") ;
309308 }
310309 }
311310
312- return new OAuth2Response ( access_token , uid , state , token_type ) ;
311+ return new OAuth2Response ( accessToken , uid , state , tokenType ) ;
313312 }
314313
315314 /// <summary>
@@ -325,7 +324,7 @@ public static OAuth2Response ParseTokenFragment(Uri redirectedUri)
325324 /// again.</param>
326325 /// <param name="client">An optional http client instance used to make requests.</param>
327326 /// <returns>The authorization response, containing the access token and uid of the authorized user</returns>
328- public async static Task < OAuth2Response > ProcessCodeFlowAsync ( string code , string appKey , string appSecret , string redirectUri , HttpClient client = null )
327+ public async static Task < OAuth2Response > ProcessCodeFlowAsync ( string code , string appKey , string appSecret , string redirectUri = null , HttpClient client = null )
329328 {
330329 if ( string . IsNullOrEmpty ( code ) )
331330 {
@@ -343,15 +342,20 @@ public async static Task<OAuth2Response> ProcessCodeFlowAsync(string code, strin
343342 var httpClient = client ?? new HttpClient ( ) ;
344343 try
345344 {
346- var content = new FormUrlEncodedContent (
347- new Dictionary < string , string >
348- {
349- { "code" , code } ,
350- { "grant_type" , "authorization_code" } ,
351- { "client_id" , appKey } ,
352- { "client_secret" , appSecret } ,
353- { "redirect_uri" , redirectUri }
354- } ) ;
345+ var parameters = new Dictionary < string , string >
346+ {
347+ { "code" , code } ,
348+ { "grant_type" , "authorization_code" } ,
349+ { "client_id" , appKey } ,
350+ { "client_secret" , appSecret }
351+ } ;
352+
353+ if ( ! string . IsNullOrEmpty ( redirectUri ) )
354+ {
355+ parameters [ "redirect_uri" ] = redirectUri ;
356+ }
357+
358+ var content = new FormUrlEncodedContent ( parameters ) ;
355359 var response = await httpClient . PostAsync ( "https://api.dropbox.com/1/oauth2/token" , content ) ;
356360
357361 var raw = await response . Content . ReadAsStringAsync ( ) ;
@@ -386,7 +390,7 @@ public async static Task<OAuth2Response> ProcessCodeFlowAsync(string code, strin
386390 /// <param name="state">The state parameter (if any) that matches that used in the initial authorize URI.</param>
387391 /// <param name="client">An optional http client instance used to make requests.</param>
388392 /// <returns>The authorization response, containing the access token and uid of the authorized user</returns>
389- public static Task < OAuth2Response > ProcessCodeFlowAsync ( Uri responseUri , string appKey , string appSecret , string redirectUri , string state = null , HttpClient client = null )
393+ public static Task < OAuth2Response > ProcessCodeFlowAsync ( Uri responseUri , string appKey , string appSecret , string redirectUri = null , string state = null , HttpClient client = null )
390394 {
391395 if ( responseUri == null )
392396 {
@@ -404,7 +408,7 @@ public static Task<OAuth2Response> ProcessCodeFlowAsync(Uri responseUri, string
404408 var query = responseUri . Query ;
405409 if ( string . IsNullOrEmpty ( query ) )
406410 {
407- throw new ArgumentException ( "responseUri" , " The redirect uri is missing expected query arguments.") ;
411+ throw new ArgumentException ( "The redirect uri is missing expected query arguments." , "responseUri ") ;
408412 }
409413
410414 query = query . TrimStart ( '?' ) ;
@@ -429,23 +433,17 @@ public static Task<OAuth2Response> ProcessCodeFlowAsync(Uri responseUri, string
429433 }
430434 else if ( state != Uri . UnescapeDataString ( elements [ 1 ] ) )
431435 {
432- throw new ArgumentException (
433- "responseUri" ,
434- "The state in the responseUri does not match the provided value." ) ;
436+ throw new ArgumentException ( "The state in the responseUri does not match the provided value." , "responseUri" ) ;
435437 }
436438 break ;
437439 default :
438- throw new ArgumentException (
439- "responseUri" ,
440- "The responseUri contains unexpected values in the query component." ) ;
440+ throw new ArgumentException ( "The responseUri contains unexpected values in the query component." , "responseUri" ) ;
441441 }
442442 }
443443
444444 if ( string . IsNullOrEmpty ( code ) )
445445 {
446- throw new ArgumentException (
447- "responseUri" ,
448- "The responseUri is missing a code value in the query component." ) ;
446+ throw new ArgumentException ( "The responseUri is missing a code value in the query component." , "responseUri" ) ;
449447 }
450448
451449 return ProcessCodeFlowAsync ( code , appKey , appSecret , redirectUri , client ) ;
@@ -460,21 +458,21 @@ public sealed class OAuth2Response
460458 /// <summary>
461459 /// Initializes a new instance of the <see cref="OAuth2Response"/> class.
462460 /// </summary>
463- /// <param name="access_token ">The access_token.</param>
461+ /// <param name="accessToken ">The access_token.</param>
464462 /// <param name="uid">The uid.</param>
465463 /// <param name="state">The state.</param>
466- /// <param name="token_type ">The token_type.</param>
467- internal OAuth2Response ( string access_token , string uid , string state , string token_type )
464+ /// <param name="tokenType ">The token_type.</param>
465+ internal OAuth2Response ( string accessToken , string uid , string state , string tokenType )
468466 {
469- if ( string . IsNullOrEmpty ( access_token ) || string . IsNullOrEmpty ( uid ) )
467+ if ( string . IsNullOrEmpty ( accessToken ) || string . IsNullOrEmpty ( uid ) )
470468 {
471469 throw new ArgumentException ( "Invalid OAuth 2.0 response, missing access_token and/or uid." ) ;
472470 }
473471
474- this . AccessToken = access_token ;
472+ this . AccessToken = accessToken ;
475473 this . Uid = uid ;
476474 this . State = state ;
477- this . TokenType = token_type ;
475+ this . TokenType = tokenType ;
478476 }
479477
480478 /// <summary>
0 commit comments