@@ -15,9 +15,6 @@ namespace SimpleTest
15
15
16
16
partial class Program
17
17
{
18
- // Add an ApiKey (from https://www.dropbox.com/developers/apps) here
19
- private const string ApiKey = "XXXXXXXXXXXXXXX" ;
20
-
21
18
// This loopback host is for demo purpose. If this port is not
22
19
// available on your machine you need to update this URL with an unused port.
23
20
private const string LoopbackHost = "http://127.0.0.1:52475/" ;
@@ -225,31 +222,49 @@ private async Task<OAuth2Response> HandleJSRedirect(HttpListener http)
225
222
/// <returns>A valid access token or null.</returns>
226
223
private async Task < string > GetAccessToken ( )
227
224
{
225
+ Settings . Default . Upgrade ( ) ;
228
226
Console . Write ( "Reset settings (Y/N) " ) ;
229
227
if ( Console . ReadKey ( ) . Key == ConsoleKey . Y )
230
228
{
231
- // Settings.Default.Reset();
229
+ Settings . Default . Reset ( ) ;
232
230
}
233
231
Console . WriteLine ( ) ;
234
232
235
-
236
- var accessToken = "" ;
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
+ }
237
250
238
- if ( string . IsNullOrEmpty ( accessToken ) )
251
+ var accessToken = Settings . Default . AccessToken ;
252
+
253
+ if ( string . IsNullOrEmpty ( accessToken ) && ! string . IsNullOrWhiteSpace ( apiKey ) )
239
254
{
240
255
try
241
256
{
242
257
Console . WriteLine ( "Waiting for credentials." ) ;
243
258
var state = Guid . NewGuid ( ) . ToString ( "N" ) ;
244
- var authorizeUri = DropboxOAuth2Helper . GetAuthorizeUri ( OAuthResponseType . Token , ApiKey , RedirectUri , state : state ) ;
259
+ var authorizeUri = DropboxOAuth2Helper . GetAuthorizeUri (
260
+ OAuthResponseType . Token , apiKey , RedirectUri , state : state ) ;
245
261
var http = new HttpListener ( ) ;
246
262
http . Prefixes . Add ( LoopbackHost ) ;
247
263
248
264
http . Start ( ) ;
249
265
250
266
// Use StartInfo to ensure default browser launches.
251
- System . Diagnostics . ProcessStartInfo startInfo =
252
- new System . Diagnostics . ProcessStartInfo ( authorizeUri . ToString ( ) ) ;
267
+ System . Diagnostics . ProcessStartInfo startInfo = new ( authorizeUri . ToString ( ) ) ;
253
268
startInfo . UseShellExecute = true ;
254
269
255
270
System . Diagnostics . Process . Start ( startInfo ) ;
@@ -275,18 +290,17 @@ private async Task<string> GetAccessToken()
275
290
var uid = result . Uid ;
276
291
Console . WriteLine ( "Uid: {0}" , uid ) ;
277
292
278
- //Settings.Default.AccessToken = accessToken;
279
- //Settings.Default.Uid = uid;
280
-
281
- //Settings.Default.Save();
293
+ Settings . Default . AccessToken = accessToken ;
294
+ Settings . Default . Uid = uid ;
282
295
}
283
296
catch ( Exception e )
284
297
{
285
298
Console . WriteLine ( "Error: {0}" , e . Message ) ;
286
299
return null ;
287
300
}
288
301
}
289
-
302
+ Settings . Default . Save ( ) ;
303
+ Settings . Default . Reload ( ) ;
290
304
return accessToken ;
291
305
}
292
306
@@ -332,7 +346,7 @@ private async Task GetCurrentAccount(DropboxClient client)
332
346
/// <param name="client">The dropbox client object.</param>
333
347
/// <param name="path">The path to the target folder to delete.</param>
334
348
/// <returns></returns>
335
- private async Task < bool > PathExists ( DropboxClient client , string path )
349
+ static private async Task < bool > PathExists ( DropboxClient client , string path )
336
350
{
337
351
try
338
352
{
@@ -351,7 +365,7 @@ private async Task<bool> PathExists(DropboxClient client, string path)
351
365
/// <param name="client">The dropbox client object.</param>
352
366
/// <param name="path">The path to the target folder to delete.</param>
353
367
/// <returns></returns>
354
- private async Task < Metadata > DeleteFolder ( DropboxClient client , string path )
368
+ static private async Task < Metadata > DeleteFolder ( DropboxClient client , string path )
355
369
{
356
370
if ( await PathExists ( client , path ) )
357
371
{
0 commit comments