1
1
namespace SimpleTest
2
2
{
3
3
using System ;
4
+ using System . Diagnostics ;
4
5
using System . IO ;
5
6
using System . Linq ;
6
7
using System . Net ;
@@ -35,27 +36,14 @@ partial class Program
35
36
private static extern bool SetForegroundWindow ( IntPtr hWnd ) ;
36
37
37
38
[ STAThread ]
38
- static int Main ( string [ ] args )
39
+ static async Task < int > Main ( )
39
40
{
40
- Console . WriteLine ( "SimpleTest" ) ;
41
- var instance = new Program ( ) ;
42
- try
43
- {
44
- var task = Task . Run ( ( Func < Task < int > > ) instance . Run ) ;
45
-
46
- task . Wait ( ) ;
47
-
48
- return task . Result ;
49
- }
50
- catch ( Exception e )
51
- {
52
- Console . WriteLine ( e ) ;
53
- throw ;
54
- }
41
+ return await Task . Run ( new Program ( ) . Run ) ;
55
42
}
56
43
57
44
private async Task < int > Run ( )
58
45
{
46
+ Console . WriteLine ( nameof ( SimpleTest ) ) ;
59
47
DropboxCertHelper . InitializeCertPinning ( ) ;
60
48
61
49
var accessToken = await this . GetAccessToken ( ) ;
@@ -66,7 +54,8 @@ private async Task<int> Run()
66
54
67
55
// Specify socket level timeout which decides maximum waiting time when no bytes are
68
56
// received by the socket.
69
- var httpClient = new HttpClient ( new HttpClientHandler ( ) )
57
+ using HttpClientHandler httpClientHandler = new HttpClientHandler ( ) ;
58
+ using var httpClient = new HttpClient ( httpClientHandler )
70
59
{
71
60
// Specify request level timeout which decides maximum time that can be spent on
72
61
// download/upload files.
@@ -230,51 +219,47 @@ private async Task<string> GetAccessToken()
230
219
}
231
220
Console . WriteLine ( ) ;
232
221
233
- string apiKey = Settings . Default . ApiKey ;
234
- while ( string . IsNullOrWhiteSpace ( apiKey ) )
235
- {
236
- Console . WriteLine ( "Create a Dropbox App at https://www.dropbox.com/developers/apps." ) ;
237
- Console . Write ( "Enter the API Key (or 'Quit' to exit): " ) ;
238
- apiKey = Console . ReadLine ( ) ;
239
- if ( apiKey . ToLower ( ) == "quit" )
240
- {
241
- Console . WriteLine ( "The API Key is required to connect to Dropbox." ) ;
242
- apiKey = "" ;
243
- break ;
244
- }
245
- else
246
- {
247
- Settings . Default . ApiKey = apiKey ;
248
- }
249
- }
222
+ string apiKey = GetApiKey ( ) ;
250
223
251
- var accessToken = Settings . Default . AccessToken ;
224
+ string accessToken = Settings . Default . AccessToken ;
252
225
253
226
if ( string . IsNullOrEmpty ( accessToken ) && ! string . IsNullOrWhiteSpace ( apiKey ) )
254
227
{
228
+ using var http = new HttpListener ( ) ;
255
229
try
256
230
{
257
231
Console . WriteLine ( "Waiting for credentials." ) ;
258
232
var state = Guid . NewGuid ( ) . ToString ( "N" ) ;
259
233
var authorizeUri = DropboxOAuth2Helper . GetAuthorizeUri (
260
234
OAuthResponseType . Token , apiKey , RedirectUri , state : state ) ;
261
- var http = new HttpListener ( ) ;
235
+
262
236
http . Prefixes . Add ( LoopbackHost ) ;
263
237
264
238
http . Start ( ) ;
265
239
266
240
// Use StartInfo to ensure default browser launches.
267
- System . Diagnostics . ProcessStartInfo startInfo = new ( authorizeUri . ToString ( ) ) ;
268
- startInfo . UseShellExecute = true ;
241
+ ProcessStartInfo startInfo =
242
+ new ProcessStartInfo ( authorizeUri . ToString ( ) ) { UseShellExecute = true } ;
269
243
270
- System . Diagnostics . Process . Start ( startInfo ) ;
244
+ try
245
+ {
246
+ // open browser for authentication
247
+ Process . Start ( startInfo ) ;
248
+ Console . WriteLine ( "Waiting for authentication..." ) ;
249
+ }
250
+ catch ( Exception )
251
+ {
252
+ Console . WriteLine ( "An unexpected error occured while opening the browser." ) ;
253
+ }
271
254
272
255
// Handle OAuth redirect and send URL fragment to local server using JS.
273
256
await HandleOAuth2Redirect ( http ) ;
274
257
275
258
// Handle redirect from JS and process OAuth response.
276
259
var result = await HandleJSRedirect ( http ) ;
277
260
261
+ http . Stop ( ) ;
262
+
278
263
if ( result . State != state )
279
264
{
280
265
// The state in the response doesn't match the state in the request.
@@ -292,18 +277,46 @@ private async Task<string> GetAccessToken()
292
277
293
278
Settings . Default . AccessToken = accessToken ;
294
279
Settings . Default . Uid = uid ;
280
+ Settings . Default . Save ( ) ;
281
+ Settings . Default . Reload ( ) ;
295
282
}
296
283
catch ( Exception e )
297
284
{
298
285
Console . WriteLine ( "Error: {0}" , e . Message ) ;
299
286
return null ;
300
287
}
301
288
}
302
- Settings . Default . Save ( ) ;
303
- Settings . Default . Reload ( ) ;
289
+
304
290
return accessToken ;
305
291
}
306
292
293
+ /// <summary>
294
+ /// Retrieve the ApiKey from the user
295
+ /// </summary>
296
+ /// <returns>Return the ApiKey specified by the user</returns>
297
+ private static string GetApiKey ( )
298
+ {
299
+ string apiKey = Settings . Default . ApiKey ;
300
+
301
+ while ( string . IsNullOrWhiteSpace ( apiKey ) )
302
+ {
303
+ Console . WriteLine ( "Create a Dropbox App at https://www.dropbox.com/developers/apps." ) ;
304
+ Console . Write ( "Enter the API Key (or 'Quit' to exit): " ) ;
305
+ apiKey = Console . ReadLine ( ) ;
306
+ if ( apiKey . ToLower ( ) == "quit" )
307
+ {
308
+ Console . WriteLine ( "The API Key is required to connect to Dropbox." ) ;
309
+ apiKey = null ;
310
+ break ;
311
+ }
312
+ else
313
+ {
314
+ Settings . Default . ApiKey = apiKey ;
315
+ }
316
+ }
317
+
318
+ return string . IsNullOrWhiteSpace ( apiKey ) ? null : apiKey ;
319
+ }
307
320
/// <summary>
308
321
/// Gets information about the currently authorized account.
309
322
/// <para>
0 commit comments