Search Posts

Xamarin.Forms Dropbox integration (app folder)

App backup / synchronization between devices

Apps usually backup their content to the respective cloud to ensure local data isn’t lost, but there is of course no guarantee the user has activated those features.

When working with Xamarin/Forms your app is available on more then one device, so you might want to sync files between the OS:s too – like an SQLite database for example.

Complete Code for this tutorial

DropboxService.cs GIST

Dropbox v2 API SDK

Dropbox got a new fancy API (v2) and what do you know – there’s an .NET SDK that supports PCL profiles and Mono alike, making Dropbox compatible with Xamarin Droid / iOS / Forms. I’m using Forms in this tutorial, but as long as you roll a PCL / NetCore library for sharing code it should work fine in either case.

I’m going to use the “app folder” type of integration, where your app gets a special folder in the user’s Dropbox, which is a good approach if you’re going to read/write files without user interaction. Alternatively you can check out the full integration, where the user can pick a folder.

Read more:
Dropbox .Net SDK API v2 official documentation

Step 1: Add Dropbox to your Xamarin project

Install the package Dropbox.Api (version 3.5.0 a.t.m) in your PCL and platform projects (with Quick Install).

Step 2: Authenticate users against Dropbox

The methodology I’m using here is a Webview (integrate a web browser in the app) to display the Dropbox login interface in a modal page and then listening to the navigation event to pick up the Dropbox user token once authentication is complete:

Important configuration here is “YourDropboxAppKey” and “YourDropboxRedirectUri“. To find these, create an app in the Dropbox Developer Console and copy the exact values from your app page.

What I’m doing is fetching an URI to the Dropbox login page for my Dropbox app and displaying this page in a Webview so the user can log in.

Step 3: Get the logged in user’s dropbox token

When the user has logged in to Dropbox the url of the Webview will contain a Token you can use to authenticate all other requests. You should save this token in app settings so you can use it for future requests.

When I’ve saved the token the Webview can be closed by calling PopModalAsync().

List write read files

Good to go! Now that we got a token we can perform common operations as described in the documentation:

Full code example:
DropboxService.cs GIST

Happy coding!

%d bloggers like this: