DK Development

The random output of a Windows developer.

DK Development

The random output of a Windows developer.




DropNetRT

DropNetRT is a Portable Class Library for .Net (including Windows Store and Windows Phone) applications to connect to the Dropbox API.

PM> Install-Package DropNetRT

Getting started

To create an app that uses Dropbox you first need to register your app with Dropbox (Developer site). This will give you the API Key and Secret combination required to use the API.

Once you have your API Key and Secret download DropNetRT from NuGet (or alternatively download the source from GitHub).

DropNetClient

To start using DropNetRT create an instance of the DropNetClient class using your API Key and Secret generated from the Dropbox developer site.

var client = new DropNetClient("API KEY", "API SECRET");

The constructor has an overload method for if you have already authenticated with the API and have an access token for the user. (covered in Authentication).

var client = new DropNetClient("API KEY", "API SECRET", "USER TOKEN", "USER SECRET");

Authentication

The dropbox API uses oauth for authenticating, it does both v1 and v2 but at the moment DropNetRT only supports oauth v1.

Authentication is a 3 step process.

  • Step 1: Get a request token from the API.
  • Step 2: Navigate the user to the dropbox site to login (using the request token).
  • Step 3: Convert the request token into an access token to use the rest of the API.


Step 1: Get a request token from the API. Call the GetRequestToken function to create a new request token. Note: The DropNetClient instance will save this as the UserLogin property but is also returned by the function.

var requestToken = await client.GetRequestToken();

Step 2: Navigate the user to the dropbox site to login (using the request token).

var url = client.BuildAuthorizeUrl(requestToken, "http://dkdevelopment.net");

From here you will need to open a browser with the url given to allow the user to login. Then wait for the callback the the given callback url before moving on. An example of this step can be found in the WP8 sample app (source on github). WinRT apps can use the WebAuthenticationBroker for this, just give it the url and callback parameters then parse the response for the token querystring parameter. (Note the secret doesn't change, only the token)

Step 3: Convert the request token into an access token to use the rest of the API. To do this call the GetAccessToken function. Note: The DropNetClient instance will save this as the UserLogin property but is also returned by the function. Save this token so the user doesn't have to reauthenticate each time.

var accessToken = await client.GetAccessToken();

App folder/Sandbox mode

To set the DropNetClient to use an app folder instead of the users full dropbox folder set the UseSandbox property to true. This may cause the client to throw exceptions if the app is set to only be allowed access to app folders.

client.UseSandbox = true;

User functions

coming soon

File functions

Copy
Copies a file or folder from one location to another.

var copiedMetadata = await client.Copy("/from-path/file", "/to-path");

Create Folder
Creates an empty folder with the given path

var folderMetadata = await client.CreateFolder("/new-folder-to-create");

Delete
Deletes a file or folder given its path

var deletedMetadata = await client.Delete("/file-to-delete");

Get Delta
Gets the Delta (changes) of a given path and cursor from the last time it was called (otherwise returns last 2000 folder changes)

var deltaPage = await client.GetDelta("cursor");

Get File
Gets a file from dropbox given the path

var fileBytes = await client.GetFile("/file-to-get");

GetMetaData
Gets info on a file or folder (including folder contents). There are a few functions for getting an items Metadata, most of them are shorthand methods of the main one: GetMetaData(string path, string hash, int? rev, bool list, bool includeDeleted)

var metadata = await client.GetMetaData("/test");

GetShare
Gets a share link for a file or folder.

var shareLink = await client.GetShare("/test");
var nonShortShareLink = await client.GetShare("/test", false);

Search
Searches for a given string.

var resultsList = await client.Search("test");
var resultsList = await client.Search("test", "/path-to-search");

Platform support

DropNetRT is a PCL (Portable Class Library) which means the same dll can be used across multiple platforms. Here are the target frameworks:

  • .NET 4.5
  • .NET for Windows Store apps (WinRT)
  • Windows Phone 7.5 and higher
  • Silverlight 4 and higher

Samples

coming soon

Upcoming features

Checkout the Trello board to whats coming

Apps using DropNetRT

Here is a list of Apps using DropNetRT. If you have an app using it let me know (@dkarzon).

  • BoxShot (as of v3.4)