This repository was archived by the owner on Jul 30, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathProgram.cs
More file actions
86 lines (70 loc) · 3.05 KB
/
Program.cs
File metadata and controls
86 lines (70 loc) · 3.05 KB
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
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using DropboxRestAPI;
namespace Sample
{
class Program
{
private static void Main()
{
Run().Wait();
}
private static async Task Run()
{
var options = new Options
{
ClientId = "...",
ClientSecret = "...",
RedirectUri = "..."
};
// Initialize a new Client (without an AccessToken)
var client = new Client(options);
// Get the OAuth Request Url
var authRequestUrl = await client.Core.OAuth2.AuthorizeAsync("code");
// TODO: Navigate to authRequestUrl using the browser, and retrieve the Authorization Code from the response
var authCode = "...";
// Exchange the Authorization Code with Access/Refresh tokens
var token = await client.Core.OAuth2.TokenAsync(authCode);
// Get account info
var accountInfo = await client.Core.Accounts.AccountInfoAsync();
Console.WriteLine("Uid: " + accountInfo.uid);
Console.WriteLine("Display_name: " + accountInfo.display_name);
Console.WriteLine("Email: " + accountInfo.email);
// Get root folder without content
var rootFolder = await client.Core.Metadata.MetadataAsync("/", list: false);
Console.WriteLine("Root Folder: {0} (Id: {1})", rootFolder.Name, rootFolder.path);
// Get root folder with content
rootFolder = await client.Core.Metadata.MetadataAsync("/", list: true);
foreach (var folder in rootFolder.contents)
{
Console.WriteLine(" -> {0}: {1} (Id: {2})",
folder.is_dir ? "Folder" : "File", folder.Name, folder.path);
}
// Initialize a new Client (with an AccessToken)
var client2 = new Client(options);
// Create a new folder
var newFolder = await client2.Core.FileOperations.CreateFolderAsync("/New Folder");
// Find a file in the root folder
var file = rootFolder.contents.FirstOrDefault(x => x.is_dir == false);
// Download a file
var tempFile = Path.GetTempFileName();
using (var fileStream = System.IO.File.OpenWrite(tempFile))
{
await client2.Core.Metadata.FilesAsync(file.path, fileStream);
}
//Upload the downloaded file to the new folder
using (var fileStream = System.IO.File.OpenRead(tempFile))
{
var uploadedFile =await client2.Core.Metadata.FilesPutAsync(fileStream, newFolder.path + "/" + file.Name);
}
// Search file based on name
var searchResults = await client2.Core.Metadata.SearchAsync("/", file.Name);
foreach (var searchResult in searchResults)
{
Console.WriteLine("Found: " + searchResult.path);
}
}
}
}