[ Update: API v2 and the Dropbox.NET SDK are no longer in preview and are ready for use in production.]
Today we're announcing the Dropbox.NET SDK, a new SDK that you can use to try out our new API v2 preview. We've built this SDK to support the Microsoft development community and we'd love to get your thoughts and feedback.
The Dropbox.NET SDK is a Portable Class Library that works with multiple platforms including Windows, Windows Phone, and Mono.
Please keep in mind that both the SDK and API v2 are in preview mode so please don’t use them for your production apps just yet. We’ll let you know when the final versions are ready.
Install the Dropbox.NET SDK
We recommend using NuGet to install the new Dropbox.NET SDK.
To install Dropbox.Api
, run the following command in the Package Manager Console:
PM> Install-Package Dropbox.Api -Pre
Register a Dropbox API app
If you haven't already, you'll need to register a new app in the App Console. Select Dropbox API app and choose your app's permission. You'll need to use the app key created with this app to access API v2.
That's it! You’re ready to try out the Dropbox.NET SDK.
Link an account
In order to make calls to the API, you'll need a DropboxClient
instance. To instantiate, pass in the access token for the account you want to link.
You can generate an access token for testing with your own account through the App Console. In order to authorize additional users, the Dropbox.NET SDK contains helper methods for using OAuth with Dropbox.
Here's an example where we set up the DropboxClient
and check the current user by calling GetCurrentAccountAsync
, which returns an instance of FullAccount
:
using System;
using System.Threading.Tasks;
using Dropbox.Api;
class Program
{
static void Main(string [] args)
{
var task = Task.Run((Func<Task>)Program.Run);
task.Wait();
}
static async Task Run()
{
using (var dbx = new DropboxClient("YOUR ACCESS TOKEN"))
{
var full = await dbx.Users.GetCurrentAccountAsync();
Console.WriteLine("{0} - {1}", full.Name.DisplayName, full.Email);
}
}
}
Try some API requests
Now that you have a DropboxClient handy, let's try making some API requests. You can use the DropboxClient
object you instantiated above to make API calls. Let's try out some of the Files requests.
To list all the contents in the user's root directory:
async Task ListRootFolder(DropboxClient dbx)
{
var list = await dbx.Files.ListFolderAsync(string.Empty);
// 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))
{
Console.WriteLine("F{0,8} {1}", item.AsFile.Size, item.Name);
}
}
To download a file:
async Task Download(DropboxClient dbx, string folder, string file)
{
using (var response = await dbx.Files.DownloadAsync(folder + "/" + file))
{
Console.WriteLine(await response.GetContentAsStringAsync());
}
}
To upload a file:
async Task Upload(DropboxClient dbx, string folder, string file, string content)
{
using (var mem = new MemoryStream(Encoding.UTF8.GetBytes(content))
{
var updated = await dbx.Files.UploadAsync(
folder + "/" + file,
WriteMode.Overwrite.Instance,
body: mem);
Console.WriteLine("Saved {0}/{1} rev {2}", folder, file, updated.Rev);
}
}