Skip to content

Commit 6907b4a

Browse files
committed
Update user agent. Support optional redirect url for oauth2 code flow. Also include minor style changes.
1 parent e07dcf9 commit 6907b4a

File tree

5 files changed

+54
-56
lines changed

5 files changed

+54
-56
lines changed

Dropbox.Api/Dropbox.Api.nuspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
<package >
33
<metadata>
44
<id>$id$</id>
5-
<version>1.1.2-beta</version>
6-
<title>Dropbox v2 API</title>
5+
<version>1.1.3</version>
6+
<title>Dropbox v2 API Beta</title>
77
<authors>Dropbox Inc</authors>
88
<owners>Dropbox Inc</owners>
99
<licenseUrl>https://github.com/dropbox/dropbox-sdk-dotnet/blob/master/LICENSE</licenseUrl>

Dropbox.Api/DropboxClient.common.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public sealed partial class DropboxClient : ITransport, IDisposable
8484
/// <summary>
8585
/// The base user agent, used to construct all user agent strings.
8686
/// </summary>
87-
private const string BaseUserAgent = "OfficialDropboxDotNetV2SDK";
87+
private const string BaseUserAgent = "OfficialDropboxDotNetSDKv2";
8888

8989
/// <summary>
9090
/// The maximum retries on a 5xx error.

Dropbox.Api/DropboxOauth2Helper.cs

Lines changed: 41 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//-----------------------------------------------------------------------------
1+
//-----------------------------------------------------------------------------
22
// <copyright file="DropboxOAuth2Helper.cs" company="Dropbox Inc">
33
// Copyright (c) Dropbox Inc. All rights reserved.
44
// </copyright>
@@ -7,9 +7,9 @@
77
namespace 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>

Examples/SimpleTest/LoginForm.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Dropbox.Api;
1+
using Dropbox.Api;
22
using System;
33
using System.Collections.Generic;
44
using System.Globalization;

Examples/SimpleTest/Program.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace SimpleTest
1+
namespace SimpleTest
22
{
33
using System;
44
using System.Linq;
@@ -34,8 +34,8 @@ private async Task<int> Run()
3434
{
3535
InitializeCertPinning();
3636

37-
var access_token = await this.GetAccessToken();
38-
if (string.IsNullOrEmpty(access_token))
37+
var accessToken = await this.GetAccessToken();
38+
if (string.IsNullOrEmpty(accessToken))
3939
{
4040
return 1;
4141
}
@@ -49,7 +49,7 @@ private async Task<int> Run()
4949
Timeout = TimeSpan.FromMinutes(20)
5050
};
5151

52-
this.client = new DropboxClient(access_token, userAgent: "SimpleTestApp", httpClient: httpClient);
52+
this.client = new DropboxClient(accessToken, userAgent: "SimpleTestApp", httpClient: httpClient);
5353

5454
try
5555
{
@@ -110,9 +110,9 @@ private async Task<string> GetAccessToken()
110110
}
111111
Console.WriteLine();
112112

113-
var access_token = Settings.Default.AccessToken;
113+
var accessToken = Settings.Default.AccessToken;
114114

115-
if (string.IsNullOrEmpty(access_token))
115+
if (string.IsNullOrEmpty(accessToken))
116116
{
117117
Console.WriteLine("Waiting for credentials.");
118118
var completion = new TaskCompletionSource<Tuple<string, string>>();
@@ -146,11 +146,11 @@ private async Task<string> GetAccessToken()
146146
var result = await completion.Task;
147147
Console.WriteLine("and back...");
148148

149-
access_token = result.Item1;
149+
accessToken = result.Item1;
150150
var uid = result.Item2;
151151
Console.WriteLine("Uid: {0}", uid);
152152

153-
Settings.Default.AccessToken = access_token;
153+
Settings.Default.AccessToken = accessToken;
154154
Settings.Default.Uid = uid;
155155

156156
Settings.Default.Save();
@@ -163,7 +163,7 @@ private async Task<string> GetAccessToken()
163163
}
164164
}
165165

166-
return access_token;
166+
return accessToken;
167167
}
168168

169169
/// <summary>

0 commit comments

Comments
 (0)