In our previous posts here and here, we looked at why and how F# can be used with WPF. Today we will check out how to create a real-world application using F# and WPF (with MVVM pattern). We would check this out using a video tutorial, (which is more intuitive and less time consuming to read a big post). Below is the app architecture,
Note: To start developing twitter apps, You should first get registered in dev.twitter.com and get the access token and secret.
There are three main areas that defines the application,
- oAuth – To get authorization with Twitter
- Download Agent – F# Agent worker that connects to Twitter with oAuth and parses the tweets from XML response stream.
- MVVM – The whole app is built around MVVM
OAuth in Twitter
OAuth requires a series of steps to execute and get the authorization key and secret. You can find more details here. In our sample we have a script file(twitter_oauth.fsx) that shows how to get the token and the secret, Execute the below code,
// Execute the below three lines first and then copy-paste the verifier code that Twitter gives you let oauth_token'', oauth_token_secret'', oauth_callback_confirmed = OAuth.requestToken() let url = OAuth.authorizeURI + "?oauth_token=" + oauth_token'' System.Diagnostics.Process.Start(url) |> ignore
This will start off the your browser to allow access to this app,
This will give you a “verifier” code which you need to use to get your private access token and secret.
let mutable verifier = "" let oauth_token, oauth_token_secret = OAuth.accessToken(oauth_token'', oauth_token_secret'', verifier) printfn "%s" oauth_token printfn "%s" oauth_token_secret
Now you are good to go ahead and start writing the Twitter app. The Twitter app contains an agent worker to pull the data from twitter and a simple observable collection to push the data into the UI.
We also use some async helpers that lets us marshal from background thread to the UI thread using SynchronizationContext. Check the “AsyncHelpers.fs” for more details.
Video
Download the source from here. The sample project doesn’t contain the consumer key / secret as well as the oauth key and secret, you need to manually replace them to run the application.