Twipler

An Open Source Twitter Web Client in ASP.NET
 

Simple Twitter web client example

Download example

Download the C# source code here: Simple.zip

You will need to Register a New Twitter Web application as well.

Simple twitter web client

In this exampe, you will see how simple it can be to create a Twitter web client using the TweetSharp library, ASP.NET and a bit of code. This will use OAuth to authenticate with Twitter, then make a few API calls.

You might find it helpful to read this post How to authenticae a web application with OAuth

TweetSharpHelper - I've provided this helper class which has 3 methods. (Note, you will have to place your ConsumerKey and ConsumerSecret into this class!

UserAuthenticateUrl returns a URL to redirect a user to twitter to Authenticate.
AuthenticateResponse is called when Twitter returns the Authenticated user to you.
Request is used to call the TweetSharp API and return the response.

Login
string redirect = Helper.UserAuthenticateUrl();
if (!string.IsNullOrEmpty(redirect))
          Response.Redirect(redirect);
Authenticate

Twitter will respond by returning the user to your "Callback Url" page. The call will be appended with the Query String oauth_token which you can pass into the AuthenticateResponse method of the helper class.

protected void Page_Load(object sender, EventArgs e)        
{
      if (Request.QueryString["oauth_token"] != null)
      {
          Helper.AuthenticateResponse(Request.QueryString["oauth_token"]);
          Response.Redirect("default.aspx");
       } 
}
Calling TweetSharp API

In this demo I've created TweetSharpHelper class, which implements a Request method. This method takes a Func<T, TResult> parameter, allowing the API to be called with a simple lambda expression.

For example, to get Statuses on Friends timeline...

string response = Helper.Request(t => t.Statuses().OnFriendsTimeline(), false);

Search for tweets containing text...

string response = Helper.Request(t => t.Search().Query().Containing(txtBox.Text), false);

So hopefully you've found this tutorial helpful, and now realise that calling into the Twitter API using TweetSharp is pretty straight forward. This demo only returns the raw results from Twitter, and TweetSharp offers extension methods like AsDirectMessages to convert the results into meaningful data structures.

If your hungry for more code like this, why not dig into the Twipler code?

Tags: tutorial
blog comments powered by Disqus
 

Try it!

Archive

 
Copyright

Copyright © 2009-2010 Ian Quigley