A few weeks ago, I decided to spend some more time experimenting with the Twitter API and TweetSharp. If you missed my last blog entry about it, you can access it here. Last time I focused on creating a simple UserControl for ASP.NET that displayed recent tweets. It didn’t require any authentication and was actually quite simple to implement. This time around, I wanted to create a RadControls for WinForms based application that retrieved information about my accounts followers. This type of information is unfortunately not publically accessible through the API. You actually need to authenticate and log in to an account to see information about its friends and followers. In this blog entry, I’ll run you through the process of setting a WinForms application up for Twitter authentication through the use of TweetSharp.
In order to develop an application that authenticates and runs against the Twitter API, Twitter actually requires that you register your application with them. In order to get started doing this, you’ll need to head over to http://dev.twitter.com/. The process for registering is actually quite simple. You’ll need to click the “Register an app” button and fill out a simple form. Since the application running against it will be a desktop application, you will of course need to select the Client radio button for Application Type.
After you’ve registered your application, a page will be displayed containing various keys that can be used to connect to the Twitter API. In the case of this application, you will need the Consumer Key and Consumer Secret.
At the beginning of September, 2010, Twitter made the switch from Basic Auth to OAuth. Basic Auth was quick and easy to use because you could literally pass a plain text username and password to authenticate with the API. However, this also made this method of authentication extremely insecure. OAuth is much more secure, but is honestly somewhat confusing to work with due to the amount of handshaking it requires. The authentication process for my application works as follows.
Figure 1. Ripper Mcgee
The code for beginning the authentication process is…
// Step 1: Retrieve an unauthorized request token
var step1 = FluentTwitter.CreateRequest()
.Configuration.UseHttps()
.Authentication.GetRequestToken(TwitterConstants.OAUTH_CONSUMER_KEY, TwitterConstants.OAUTH_CONSUMER_SECRET);
var response = step1.Request();
var unauthorizedToken = response.AsToken();
// Step 2: Calling this method will launch the authorization page in a browser
var step2 = FluentTwitter.CreateRequest()
.Authentication.AuthorizeDesktop(
TwitterConstants.OAUTH_CONSUMER_KEY,
TwitterConstants.OAUTH_CONSUMER_SECRET,
unauthorizedToken.Token);
Figure 2. Logging In
Figure 3. The Authentication PIN
Figure 4. Authorizing the Application
The code for completing the authentication process is…
// Step 3: Display dialog to the user requesting the PIN
PINDialog dlg =
new
PINDialog();
dlg.ShowDialog();
string
pin = dlg.PIN.Trim();
// Step 4: Retrieve the OAuthToken
var finalTwitter = FluentTwitter.CreateRequest()
.Authentication.GetAccessToken(TwitterConstants.OAUTH_CONSUMER_KEY,
TwitterConstants.OAUTH_CONSUMER_SECRET,
unauthorizedToken.Token,
pin);
var accessResponse = finalTwitter.Request();
OAuthToken TWITTER_OAUTH = accessResponse.AsToken();
return
TWITTER_OAUTH;
Figure 5. Scanning for Data
After the authorization process has been completed, you can click the Scan for Data button to scan for information about your followers. Clicking the Export Data button allows you to export an excel spreadsheet using the Export to Excel feature of the RadGridView for WinForms.
Click here to download the code used in this post.