Dropbox.NET SDK preview for API v2

Dropbox has released a preview of the Dropbox.NET SDK, built to work with the new API v2. The SDK is a Portable Class Library that supports Windows, Windows Phone, and Mono. Both the SDK and API v2 are currently in preview and not intended for production use.

The SDK is available via NuGet. Install the Dropbox.Api package from the Package Manager Console:

PM> Install-Package Dropbox.Api -Pre

Getting started

You first need a Dropbox API app registered in the App Console. Choose Dropbox API app and select the permission level. The app key you receive is used to access API v2.

Authenticating with OAuth and making requests

To call the API, create a DropboxClient instance and pass in the access token for the account you want to link. For your own test account, generate an access token from the App Console. The SDK provides helper methods for OAuth authorization of additional users.

With the client instantiated, basic file operations use the Files routes. For example, to inspect the current account:

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);
        }
    }
}

List the root folder contents:

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);
    }
}

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());
    }
}

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);
    }
}

API reference

Full SDK documentation, including details on all routes and types, is available in the Dropbox.NET SDK documentation.