Tech Blog

Jay's Technical blog

Why MetroOAuth? or C# Async makes life easier

18 July 2012
Jay Kimble

[WARNING! This is an archived post and as such there may be things broken/missing here.. you have been warned.]

Yesterday, I finally pushed some of the oAuth libraries I have been tinkering with. I have a really nice and simple library (and this simplicity seems to flow throughout the library). I decided to pull out some examples using my DropboxClient to show how much simpler a library can be.

Here’s how you log in to Dropbox (in c#):

// if the user has already auth'd you can save this in ISO Store or localstorage
var accessKey = ""; // in our case we are setting it to empty (but it's not required)
var client = new DropboxClient(kDropBoxAppKey, kDropBoxAppSecret, accessKey);
var authd = false;
if (client.IsAuthenticated) // do we already have an access key
  authd = true;
  authd = await App.DropboxApi.Authorize(""); // URI to go to once logged in
// Do something

We put everything into that example just so you can see the full set up. You don’t need the accessKey for instance and can simply leave it off that call (it’s optional). Like the comment says, after the user has Authorized your app you can grab that value from the client (stored in a property) and save it off, and then the user will not need to re-authenticate.

The “if” statement where we check if the user is already authenticated is really a convenience method, but you don’t want to re-authorize the user.

The only lines you really need are the creation of the DropboxClient and the line to Authorize.

Now what is behind that Authorize call is what makes this amazing to me. There are 2 http calls that happen with a call to the WebAuthenticationBroker to show the user a web browser window that is on the Dropbox site. All of these things happen asynchronously, but they happen in order. So, there is a call to get a temporary key which we use with the URI that we push the user to the Dropbox site’s login. After they have logged in (or closed the windows), we check to see if we got a code from the call. If so we make a call to get our official accessKey, and we can proceed from there. Again, all of this happens asynchronously, and your UI will be responsive during this time.

Async makes it so we don’t have to set up any kind of response action or event, etc. Each of the calls returns a Task or a Task<T>. We simply tell the compiler that we want to wait for the task that was passed back to us to complete before we proceed. Inside of the task that we get back there might be other tasks that get awaited. This makes it possible to have a method have a single responsibility without having to clutter it up with what happens or a link to what will happen on return.


AND, all this works in Metro and WP7.x (as long as you install the Async CTP)

Anyway, I’ll blog some more on this soon.

BTW, my project is at and the current (rather cluttered) code repo is at