-
Notifications
You must be signed in to change notification settings - Fork 451
/
Program.cs
533 lines (452 loc) · 20.3 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
namespace SimpleTest
{
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Dropbox.Api;
using Dropbox.Api.Common;
using Dropbox.Api.Files;
using Dropbox.Api.Team;
partial class Program
{
// Add an ApiKey (from https://www.dropbox.com/developers/apps) here
private const string ApiKey = "XXXXXXXXXXXXXXX";
// This loopback host is for demo purpose. If this port is not
// available on your machine you need to update this URL with an unused port.
private const string LoopbackHost = "http://127.0.0.1:52475/";
// URL to receive OAuth 2 redirect from Dropbox server.
// You also need to register this redirect URL on https://www.dropbox.com/developers/apps.
private readonly Uri RedirectUri = new Uri(LoopbackHost + "authorize");
// URL to receive access token from JS.
private readonly Uri JSRedirectUri = new Uri(LoopbackHost + "token");
[DllImport("kernel32.dll", ExactSpelling = true)]
private static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[STAThread]
static int Main(string[] args)
{
Console.WriteLine("SimpleTest");
var instance = new Program();
try
{
var task = Task.Run((Func<Task<int>>)instance.Run);
task.Wait();
return task.Result;
}
catch (Exception e)
{
Console.WriteLine(e);
throw e;
}
}
private async Task<int> Run()
{
var accessToken = await this.GetAccessToken();
if (string.IsNullOrEmpty(accessToken))
{
return 1;
}
// Specify socket level timeout which decides maximum waiting time when no bytes are
// received by the socket.
var httpClient = new HttpClient(new WebRequestHandler { ReadWriteTimeout = 10 * 1000 })
{
// Specify request level timeout which decides maximum time that can be spent on
// download/upload files.
Timeout = TimeSpan.FromMinutes(20)
};
try
{
var config = new DropboxClientConfig("SimpleTestApp")
{
HttpClient = httpClient
};
var client = new DropboxClient(accessToken, config);
await RunUserTests(client);
// Tests below are for Dropbox Business endpoints. To run these tests, make sure the ApiKey is for
// a Dropbox Business app and you have an admin account to log in.
/*
var client = new DropboxTeamClient(accessToken, userAgent: "SimpleTeamTestApp", httpClient: httpClient);
await RunTeamTests(client);
*/
}
catch (HttpException e)
{
Console.WriteLine("Exception reported from RPC layer");
Console.WriteLine(" Status code: {0}", e.StatusCode);
Console.WriteLine(" Message : {0}", e.Message);
if (e.RequestUri != null)
{
Console.WriteLine(" Request uri: {0}", e.RequestUri);
}
}
return 0;
}
/// <summary>
/// Run tests for user-level operations.
/// </summary>
/// <param name="client">The Dropbox client.</param>
/// <returns>An asynchronous task.</returns>
private async Task RunUserTests(DropboxClient client)
{
await GetCurrentAccount(client);
var path = "/DotNetApi/Help";
var folder = await CreateFolder(client, path);
var list = await ListFolder(client, path);
var firstFile = list.Entries.FirstOrDefault(i => i.IsFile);
if (firstFile != null)
{
await Download(client, path, firstFile.AsFile);
}
var pathInTeamSpace = "/Test";
await ListFolderInTeamSpace(client, pathInTeamSpace);
await Upload(client, path, "Test.txt", "This is a text file");
await ChunkUpload(client, path, "Binary");
}
/// <summary>
/// Run tests for team-level operations.
/// </summary>
/// <param name="client">The Dropbox client.</param>
/// <returns>An asynchronous task.</returns>
private async Task RunTeamTests(DropboxTeamClient client)
{
var members = await client.Team.MembersListAsync();
var member = members.Members.FirstOrDefault();
if (member != null)
{
// A team client can perform action on a team member's behalf. To do this,
// just pass in team member id in to AsMember function which returns a user client.
// This client will operates on this team member's Dropbox.
var userClient = client.AsMember(member.Profile.TeamMemberId);
await RunUserTests(userClient);
}
}
/// <summary>
/// Handles the redirect from Dropbox server. Because we are using token flow, the local
/// http server cannot directly receive the URL fragment. We need to return a HTML page with
/// inline JS which can send URL fragment to local server as URL parameter.
/// </summary>
/// <param name="http">The http listener.</param>
/// <returns>The <see cref="Task"/></returns>
private async Task HandleOAuth2Redirect(HttpListener http)
{
var context = await http.GetContextAsync();
// We only care about request to RedirectUri endpoint.
while (context.Request.Url.AbsolutePath != RedirectUri.AbsolutePath)
{
context = await http.GetContextAsync();
}
context.Response.ContentType = "text/html";
// Respond with a page which runs JS and sends URL fragment as query string
// to TokenRedirectUri.
using (var file = File.OpenRead("index.html"))
{
file.CopyTo(context.Response.OutputStream);
}
context.Response.OutputStream.Close();
}
/// <summary>
/// Handle the redirect from JS and process raw redirect URI with fragment to
/// complete the authorization flow.
/// </summary>
/// <param name="http">The http listener.</param>
/// <returns>The <see cref="OAuth2Response"/></returns>
private async Task<OAuth2Response> HandleJSRedirect(HttpListener http)
{
var context = await http.GetContextAsync();
// We only care about request to TokenRedirectUri endpoint.
while (context.Request.Url.AbsolutePath != JSRedirectUri.AbsolutePath)
{
context = await http.GetContextAsync();
}
var redirectUri = new Uri(context.Request.QueryString["url_with_fragment"]);
var result = DropboxOAuth2Helper.ParseTokenFragment(redirectUri);
return result;
}
/// <summary>
/// Gets the dropbox access token.
/// <para>
/// This fetches the access token from the applications settings, if it is not found there
/// (or if the user chooses to reset the settings) then the UI in <see cref="LoginForm"/> is
/// displayed to authorize the user.
/// </para>
/// </summary>
/// <returns>A valid access token or null.</returns>
private async Task<string> GetAccessToken()
{
Console.Write("Reset settings (Y/N) ");
if (Console.ReadKey().Key == ConsoleKey.Y)
{
Settings.Default.Reset();
}
Console.WriteLine();
var accessToken = Settings.Default.AccessToken;
if (string.IsNullOrEmpty(accessToken))
{
try
{
Console.WriteLine("Waiting for credentials.");
var state = Guid.NewGuid().ToString("N");
var authorizeUri = DropboxOAuth2Helper.GetAuthorizeUri(OAuthResponseType.Token, ApiKey, RedirectUri, state: state);
var http = new HttpListener();
http.Prefixes.Add(LoopbackHost);
http.Start();
System.Diagnostics.Process.Start(authorizeUri.ToString());
// Handle OAuth redirect and send URL fragment to local server using JS.
await HandleOAuth2Redirect(http);
// Handle redirect from JS and process OAuth response.
var result = await HandleJSRedirect(http);
if (result.State != state)
{
// The state in the response doesn't match the state in the request.
return null;
}
Console.WriteLine("and back...");
// Bring console window to the front.
SetForegroundWindow(GetConsoleWindow());
accessToken = result.AccessToken;
var uid = result.Uid;
Console.WriteLine("Uid: {0}", uid);
Settings.Default.AccessToken = accessToken;
Settings.Default.Uid = uid;
Settings.Default.Save();
}
catch (Exception e)
{
Console.WriteLine("Error: {0}", e.Message);
return null;
}
}
return accessToken;
}
/// <summary>
/// Gets information about the currently authorized account.
/// <para>
/// This demonstrates calling a simple rpc style api from the Users namespace.
/// </para>
/// </summary>
/// <param name="client">The Dropbox client.</param>
/// <returns>An asynchronous task.</returns>
private async Task GetCurrentAccount(DropboxClient client)
{
var full = await client.Users.GetCurrentAccountAsync();
Console.WriteLine("Account id : {0}", full.AccountId);
Console.WriteLine("Country : {0}", full.Country);
Console.WriteLine("Email : {0}", full.Email);
Console.WriteLine("Is paired : {0}", full.IsPaired ? "Yes" : "No");
Console.WriteLine("Locale : {0}", full.Locale);
Console.WriteLine("Name");
Console.WriteLine(" Display : {0}", full.Name.DisplayName);
Console.WriteLine(" Familiar : {0}", full.Name.FamiliarName);
Console.WriteLine(" Given : {0}", full.Name.GivenName);
Console.WriteLine(" Surname : {0}", full.Name.Surname);
Console.WriteLine("Referral link : {0}", full.ReferralLink);
if (full.Team != null)
{
Console.WriteLine("Team");
Console.WriteLine(" Id : {0}", full.Team.Id);
Console.WriteLine(" Name : {0}", full.Team.Name);
}
else
{
Console.WriteLine("Team - None");
}
}
/// <summary>
/// Creates the specified folder.
/// </summary>
/// <remarks>This demonstrates calling an rpc style api in the Files namespace.</remarks>
/// <param name="path">The path of the folder to create.</param>
/// <param name="client">The Dropbox client.</param>
/// <returns>The result from the ListFolderAsync call.</returns>
private async Task<FolderMetadata> CreateFolder(DropboxClient client, string path)
{
Console.WriteLine("--- Creating Folder ---");
var folderArg = new CreateFolderArg(path);
try
{
var folder = await client.Files.CreateFolderV2Async(folderArg);
Console.WriteLine("Folder: " + path + " created!");
return folder.Metadata;
}
catch (ApiException<CreateFolderError> e)
{
if (e.Message.StartsWith("path/conflict/folder"))
{
Console.WriteLine("Folder already exists... Skipping create");
return null;
}
else
{
throw e;
}
}
}
/// <summary>
/// Lists the items within a folder inside team space. See
/// https://www.dropbox.com/developers/reference/namespace-guide for details about
/// user namespace vs team namespace.
/// </summary>
/// <param name="client">The Dropbox client.</param>
/// <param name="path">The path to list.</param>
/// <returns>The <see cref="Task"/></returns>
private async Task ListFolderInTeamSpace(DropboxClient client, string path)
{
// Fetch root namespace info from user's account info.
var account = await client.Users.GetCurrentAccountAsync();
if (!account.RootInfo.IsTeam)
{
Console.WriteLine("This user doesn't belong to a team with shared space.");
}
else
{
try
{
// Point path root to namespace id of team space.
client = client.WithPathRoot(new PathRoot.Root(account.RootInfo.RootNamespaceId));
await ListFolder(client, path);
}
catch (PathRootException ex)
{
Console.WriteLine(
"The user's root namespace ID has changed to {0}",
ex.ErrorResponse.AsInvalidRoot.Value);
}
}
}
/// <summary>
/// Lists the items within a folder.
/// </summary>
/// <remarks>This demonstrates calling an rpc style api in the Files namespace.</remarks>
/// <param name="path">The path to list.</param>
/// <param name="client">The Dropbox client.</param>
/// <returns>The result from the ListFolderAsync call.</returns>
private async Task<ListFolderResult> ListFolder(DropboxClient client, string path)
{
Console.WriteLine("--- Files ---");
var list = await client.Files.ListFolderAsync(path);
// show folders then files
foreach (var item in list.Entries.Where(i => i.IsFolder))
{
Console.WriteLine("D {0}/", item.Name);
}
foreach (var item in list.Entries.Where(i => i.IsFile))
{
var file = item.AsFile;
Console.WriteLine("F{0,8} {1}",
file.Size,
item.Name);
}
if (list.HasMore)
{
Console.WriteLine(" ...");
}
return list;
}
/// <summary>
/// Downloads a file.
/// </summary>
/// <remarks>This demonstrates calling a download style api in the Files namespace.</remarks>
/// <param name="client">The Dropbox client.</param>
/// <param name="folder">The folder path in which the file should be found.</param>
/// <param name="file">The file to download within <paramref name="folder"/>.</param>
/// <returns></returns>
private async Task Download(DropboxClient client, string folder, FileMetadata file)
{
Console.WriteLine("Download file...");
using (var response = await client.Files.DownloadAsync(folder + "/" + file.Name))
{
Console.WriteLine("Downloaded {0} Rev {1}", response.Response.Name, response.Response.Rev);
Console.WriteLine("------------------------------");
Console.WriteLine(await response.GetContentAsStringAsync());
Console.WriteLine("------------------------------");
}
}
/// <summary>
/// Uploads given content to a file in Dropbox.
/// </summary>
/// <param name="client">The Dropbox client.</param>
/// <param name="folder">The folder to upload the file.</param>
/// <param name="fileName">The name of the file.</param>
/// <param name="fileContent">The file content.</param>
/// <returns></returns>
private async Task Upload(DropboxClient client, string folder, string fileName, string fileContent)
{
Console.WriteLine("Upload file...");
using (var stream = new MemoryStream(System.Text.UTF8Encoding.UTF8.GetBytes(fileContent)))
{
var response = await client.Files.UploadAsync(folder + "/" + fileName, WriteMode.Overwrite.Instance, body: stream);
Console.WriteLine("Uploaded Id {0} Rev {1}", response.Id, response.Rev);
}
}
/// <summary>
/// Uploads a big file in chunk. The is very helpful for uploading large file in slow network condition
/// and also enable capability to track upload progerss.
/// </summary>
/// <param name="client">The Dropbox client.</param>
/// <param name="folder">The folder to upload the file.</param>
/// <param name="fileName">The name of the file.</param>
/// <returns></returns>
private async Task ChunkUpload(DropboxClient client, string folder, string fileName)
{
Console.WriteLine("Chunk upload file...");
// Chunk size is 128KB.
const int chunkSize = 128 * 1024;
// Create a random file of 1MB in size.
var fileContent = new byte[1024 * 1024];
new Random().NextBytes(fileContent);
using (var stream = new MemoryStream(fileContent))
{
int numChunks = (int)Math.Ceiling((double)stream.Length / chunkSize);
byte[] buffer = new byte[chunkSize];
string sessionId = null;
for (var idx = 0; idx < numChunks; idx++)
{
Console.WriteLine("Start uploading chunk {0}", idx);
var byteRead = stream.Read(buffer, 0, chunkSize);
using (MemoryStream memStream = new MemoryStream(buffer, 0, byteRead))
{
if (idx == 0)
{
var result = await client.Files.UploadSessionStartAsync(body: memStream);
sessionId = result.SessionId;
}
else
{
UploadSessionCursor cursor = new UploadSessionCursor(sessionId, (ulong)(chunkSize * idx));
if (idx == numChunks - 1)
{
await client.Files.UploadSessionFinishAsync(cursor: cursor, commit: new CommitInfo(folder + "/" + fileName), body: memStream);
}
else
{
await client.Files.UploadSessionAppendV2Async(cursor, body: memStream);
}
}
}
}
}
}
/// <summary>
/// List all members in the team.
/// </summary>
/// <param name="client">The Dropbox team client.</param>
/// <returns>The result from the MembersListAsync call.</returns>
private async Task<MembersListResult> ListTeamMembers(DropboxTeamClient client)
{
var members = await client.Team.MembersListAsync();
foreach (var member in members.Members)
{
Console.WriteLine("Member id : {0}", member.Profile.TeamMemberId);
Console.WriteLine("Name : {0}", member.Profile.Name);
Console.WriteLine("Email : {0}", member.Profile.Email);
}
return members;
}
}
}