title | layout |
---|---|
DropNet |
layout |
DropNet is a .NET client library for the Dropbox API.
- NuGet: PM> Install-Package DropNet (NuGet Gallery)
- Download the source from github: dkarzon/DropNet
- .NET 3.5
- Windows Phone 7.1 (Mango)
To use DropNet you need an instance of the DropNetClient class, this class does everything for DropNet. This class takes the API Key and API Secret (These must be obtained from Dropbox to access the API).
{% highlight csharp %}_client = new DropNetClient("API KEY", "API SECRET");{% endhighlight %}
Dropbox now requires a web authentication to get a usable token/secret, so this is a 3 step process.
1. Get Request Token – This step gets an oauth token from dropbox (NOTE: the token must pass the other steps before it can be used)
{% highlight csharp %}// Sync _client.GetToken();
// Async _client.GetTokenAsync((userLogin) => { //Dont really need to do anything with userLogin, DropNet takes care of it for now }, (error) => { //Handle error });{% endhighlight %}
2. Authorize App with Dropbox – This step involves sending the user to a login page on the dropbox site and having them authenticate there. The DropNet client has a function to return the url for you but the rest must be handled in app, this function also takes a callback url for redirecting the user to after they have logged in. (NOTE: The token still cant be used yet.)
{% highlight csharp %}var url = _client.BuildAuthorizeUrl(); //Use the url in a browser so the user can login{% endhighlight %}
2.5 Open a browser with the url returned by BuildAuthorizeUrl – After we have the authorize url we need to direct the user there (use some sort of browser here depending on the platform) and navigate the user to the url. This will prompt them to login and authorize your app with the API.
3. Get an Access Token from the Request Token – This is the last stage of the process, converting the oauth request token into a usable dropbox API token. This function will use the clients stored Request Token but this can be overloaded if you need to specify a token to use.
{% highlight csharp %}// Sync var accessToken = _client.GetAccessToken(); //Store this token for "remember me" function
// Async _client.GetAccessTokenAsync((accessToken) => { //Store this token for "remember me" function }, (error) => { //Handle error });{% endhighlight %}
Best Practices: Dropbox’s Developer page states several times in bold red font that applications should not store a users Dropbox password and to help enforce this DropNet allows you to manually set a users Token and Secret on the client.
{% highlight csharp %}_client = new DropNetClient("API KEY", "API SECRET", "USER TOKEN", "USER SECRET"); // OR _client = new DropNetClient("API KEY", "API SECRET"); _client.UserLogin = new UserLogin { Token = "USER TOKEN", Secret = "USER SECRET" };{% endhighlight %}
The Dropbox API uses Metadata to navigate through the its folder structure. Metadata gives us access to file and folder details (such as modified dates, file size, folder contents, etc.)
{% highlight csharp %}// Sync var metaData = _client.GetMetaData("/Public"); //Folder
// Async _client.GetMetaDataAsync("/Public", (metaData) => { //Do something with MetaData }, (error) => { //Do something on error });{% endhighlight %}
The Get File is where files can be downloaded from Dropbox, just give DropNet a path and it will download your file.
{% highlight csharp %}// Sync var fileBytes = _client.GetFile("/Getting Started.rtf");
// Async _client.GetFileAsync("/Getting Started.rtf", (response) => { //Do something with response }, (error) => { //Do something on error });{% endhighlight %}
DropNet has a few variations of the Upload File function, some taking a FileInfo object, a file path or a raw byte array.
{% highlight csharp %}// Sync var uploaded = _client.UploadFile("/", "test.txt", content); //FileInfo
// Async _client.UploadFileAsync("/", "test.txt", content, (response) => { //Do something with response }, (error) => { //Do something on error });{% endhighlight %}
Copy and Move functions are simple, give DropNet a source path and a destination path and it will move the file or folder from the source path to the destination path.
{% highlight csharp %}// Sync _client.Move("/Test.txt", "/Public/Test.txt");
// Async _client.MoveAsync("/Test.txt", "/Public/Test.txt", (response) => { //Do something }, (error) => { //Do something on error });{% endhighlight %}
Delete deletes a a specified file or folder from Dropbox. (Its OK, they have a backup…)
{% highlight csharp %}// Sync _client.Delete("/Test.txt");
// Async _client.DeleteAsync("/Test.txt", (response) => { //Do something }, (error) => { //Do something on error });{% endhighlight %}
Gets your account info from Dropbox (mainly used for Quota information)
{% highlight csharp %}// Sync var accountInfo = _client.Account_Info();
// Async _client.Account_InfoAsync((accountInfo) => { //Do something with accountInfo }, (error) => { //Do something on error });{% endhighlight %}
This creates a new folder on Dropbox at the specified path
{% highlight csharp %}// Sync var metaData = _client.CreateFolder("NewFolder1");
// Async _client.CreateFolderAsync("NewFolder1", (metaData) => { //Do something with metaData }, (error) => { //Do something on error });{% endhighlight %}
This creates a temporary public link to a file.
{% highlight csharp %}// Sync var shareResponse = _client.GetShare("/Getting Started.rtf");
// Async _client.GetShareAsync("/Getting Started.rtf", (shareResponse) => { //Do something with shareResponse }, (error) => { //Do something on error });{% endhighlight %}
Gets an image thumbnail for a file in Dropbox. (Can set thumbnail size, defaults to small, 32px)
{% highlight csharp %}//Sync var rawBytes = _client.GetThumbnail("/Temp/Test.png");
//Async _client.GetThumbnailAsync("/Temp/Test.png", (rawBytes) => { //Do something with rawBytes }, (error) => { //Do something on error });{% endhighlight %}
Gets a media link for a media file in Dropbox. (used for streaming)
{% highlight csharp %}//Sync var medialink = _client.GetMedia("/Temp/AwesomeVideo1.mp4");
//Async _client.GetMediaAsync("/Temp/AwesomeVideo1.mp4", (medialink) => { //Do something with medialink }, (error) => { //Do something on error });{% endhighlight %}
Made something with DropNet? Tell me about it and I’ll list it here. (@dkarzon)
If you have any questions about DropNet post a question on Stack Overflow - http://stackoverflow.com/questions/tagged/dropnet