11namespace SimpleTest
22{
33 using System ;
4+ using System . Diagnostics ;
45 using System . IO ;
56 using System . Linq ;
67 using System . Net ;
@@ -35,27 +36,14 @@ partial class Program
3536 private static extern bool SetForegroundWindow ( IntPtr hWnd ) ;
3637
3738 [ STAThread ]
38- static int Main ( string [ ] args )
39+ static async Task < int > Main ( )
3940 {
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 ) ;
5542 }
5643
5744 private async Task < int > Run ( )
5845 {
46+ Console . WriteLine ( nameof ( SimpleTest ) ) ;
5947 DropboxCertHelper . InitializeCertPinning ( ) ;
6048
6149 var accessToken = await this . GetAccessToken ( ) ;
@@ -66,7 +54,8 @@ private async Task<int> Run()
6654
6755 // Specify socket level timeout which decides maximum waiting time when no bytes are
6856 // received by the socket.
69- var httpClient = new HttpClient ( new HttpClientHandler ( ) )
57+ using HttpClientHandler httpClientHandler = new HttpClientHandler ( ) ;
58+ using var httpClient = new HttpClient ( httpClientHandler )
7059 {
7160 // Specify request level timeout which decides maximum time that can be spent on
7261 // download/upload files.
@@ -230,51 +219,47 @@ private async Task<string> GetAccessToken()
230219 }
231220 Console . WriteLine ( ) ;
232221
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 ( ) ;
250223
251- var accessToken = Settings . Default . AccessToken ;
224+ string accessToken = Settings . Default . AccessToken ;
252225
253226 if ( string . IsNullOrEmpty ( accessToken ) && ! string . IsNullOrWhiteSpace ( apiKey ) )
254227 {
228+ using var http = new HttpListener ( ) ;
255229 try
256230 {
257231 Console . WriteLine ( "Waiting for credentials." ) ;
258232 var state = Guid . NewGuid ( ) . ToString ( "N" ) ;
259233 var authorizeUri = DropboxOAuth2Helper . GetAuthorizeUri (
260234 OAuthResponseType . Token , apiKey , RedirectUri , state : state ) ;
261- var http = new HttpListener ( ) ;
235+
262236 http . Prefixes . Add ( LoopbackHost ) ;
263237
264238 http . Start ( ) ;
265239
266240 // 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 } ;
269243
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+ }
271254
272255 // Handle OAuth redirect and send URL fragment to local server using JS.
273256 await HandleOAuth2Redirect ( http ) ;
274257
275258 // Handle redirect from JS and process OAuth response.
276259 var result = await HandleJSRedirect ( http ) ;
277260
261+ http . Stop ( ) ;
262+
278263 if ( result . State != state )
279264 {
280265 // The state in the response doesn't match the state in the request.
@@ -292,18 +277,46 @@ private async Task<string> GetAccessToken()
292277
293278 Settings . Default . AccessToken = accessToken ;
294279 Settings . Default . Uid = uid ;
280+ Settings . Default . Save ( ) ;
281+ Settings . Default . Reload ( ) ;
295282 }
296283 catch ( Exception e )
297284 {
298285 Console . WriteLine ( "Error: {0}" , e . Message ) ;
299286 return null ;
300287 }
301288 }
302- Settings . Default . Save ( ) ;
303- Settings . Default . Reload ( ) ;
289+
304290 return accessToken ;
305291 }
306292
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+ }
307320 /// <summary>
308321 /// Gets information about the currently authorized account.
309322 /// <para>
0 commit comments