OAuth with Silverlight for Windows Phone 7

May 29, 2010 — 1 Comment

In my last post I wrote about the Silverlight WebBrowser control on Windows Phone 7 and how to communicate with a web page hosted inside the control and your Windows Phone 7 app.  The example in that post was pretty trivial, so why not try something like OAuth and Twitter.

For more background on how OAuth and Twitter work check the Authentication Overview on dev.twitter.com.

In short here is the process (overly simplified) to authenticate a twitter user

  1. Register your app with Twitter and get a key
  2. Within app, get an request token
  3. With the WebBrowser control, navigate to Authentication URL
  4. Wait till the user authenticates.
  5. Get an access token
  6. Make secure calls

After becoming familiar with OAuth, first thing I decided to do was find an OAuth library for Silverlight.  The one I found most useful and easily portable to Windows Phone 7 was sample code in the book Professional Twitter Development by Daniel Crenna.  Unfortunately, this did  not compile without a few changes on WP7.  Here is a quick summary of the changes I made

  1. Change all HttpWebRequests to be async since sync requests are not supported
  2. Added callback param to all methods to get responses back to the UI
  3. Created and OAuthEventArgs class
  4. Modified support to POST web requests
  5. Added helper methods to ParseQueryString and HexEscape characters
  6. Added a TwitterOAuth class to contain all tokens, Urls and various other items.

Next thing I had to do was wire up the code to enable the user to authenticate via OAuth with Twitter.  Basically all you have to do is get a “request token” then navigate to the “authentication Url” for the user to enter their credentials.  Here is the code

When asking for a request token, you can specify a callback url that twitter will navigate to when the user successfully authenticates.  Next step is to create an HTML page that will invoke a method within our WP7 application.  The HTML Page is as follows and is very straight forward.

The method inside our WP7 app will parse the url which contains details on the authentication then goes out and gets the access token to make secure calls

Now that we are authenticated we can make a ‘Tweet’ from with our application as follows

Here is a screen shot of a test tweet

image One of the advantages of using OAuth with Twitter is you get your ‘application name’ in the ‘via’ section instead of it saying ‘posted from web’.  Also, since Basic Authentication is no longer supported by Twitter, your only option is either OAuth or XAuth.  Considering with XAuth you have to email twitter and convince them to give you access to that, OAuth is the easier way to go.

Here are some screen shots of the Windows Phone 7 client.

wp7oauthtwitter

 

You can download the source code here.  I have no plans on building a Twitter client for Windows Phone 7 as I have a feeling someone out there is probably building one :)

Any feedback let me know via here or via Twitter.

NOTES: The app will not compile until you remove the #error to add your registered app key.  Also, make sure you change the timezone and set the correct time on the emulator or else the OAuth signature generation will not work.

One response to OAuth with Silverlight for Windows Phone 7

  1. Awesome example. Thanks so much.

Leave a Reply