KeyLimeTie Blog

Twitterizer Simplifies .NET Integration with Twitter

By Peter Morano – 5/1/2009 7:56:26 AM. Posted to Applications.

As Twitter continues to grow in popularity, integrating your applications will become an essential step in effectively reaching a community. Fortunately, connecting your .NET applications with Twitter can be a trivial task if you use the Twitterizer Framework, a .NET 2.0 library that provides an easy to use, object-oriented interface to twitter's online API. This article and sample application will show you how to take advantage of this interface.

Sample Application

Getting Started

There are several steps required to get started:

1. Register your application with Twitter: http://twitter.com/oauth_clients/new. You can skip this step, but any status posts your application makes to Twitter will appear as coming from Twitterizer.
 
2. Download the Twitterizer Framework:  http://code.google.com/p/twitterizer/ and add a reference in your project.

Taking Control of Twitter

The Twitter API allows programmatic access to updating, searching and deleting a user’s status, adding and removing friends and followers, and direct messaging other others. The following examples show some of these operations.

Establish a Connection
To establish a connection, you instantiate the Twitter object as shown below (If you skip step one above, you do not need to supply the Source parameter).

                Twitter twit = null;
                twit = new Twitter("twitteruser", "twitterpwd", "app_source");


Updating Your Status

                TwitterStatus ts = twit.Status.Update("My Message");


Search and Destroy

                TwitterStatusCollection myStatus = twit.Status.UserTimeline();
                Console.WriteLine("My Status");
                foreach (TwitterStatus status in myStatus)
                {
                    Console.WriteLine(status.Text);

                    if (status.Text == "My Message")
                        twit.Status.Destroy(status.ID);
                }
                Console.WriteLine();

Search for Another User’s Status
To get the status entries for another user, you can use the TwitterParameters object, setting optional parameter values.

                TwitterParameters paras = new TwitterParameters();
                paras.Add(TwitterParameterNames.ID, UsersID);
                paras.Add(TwitterParameterNames.Since,
                     Convert.ToDateTime("1/1/2009"));
                paras.Add(TwitterParameterNames.Page, 1);
                paras.Add(TwitterParameterNames.Count, 5);
                paras.Add(TwitterParameterNames.SinceID, 1);

                TwitterStatusCollection usersStatus =
                      twit.Status.UserTimeline(paras);

                Console.WriteLine("Status For Other User");
                foreach (TwitterStatus status in usersStatus)
                {
                    Console.WriteLine(string.Format("Status For User {0}: {1}", 
                          status.TwitterUser.ScreenName, status.Text));
                }
                Console.WriteLine();

Finding Out About Your Friends (and ditching one)

                TwitterUserCollection friends = twit.User.Friends();
                foreach (TwitterUser friend in friends)
                {
                    Console.WriteLine(friend.ScreenName.ToString());
                    Console.WriteLine(friend.Status.Text.ToString());
                    Console.WriteLine(friend.NumberOfFollowers.ToString());

                    //Remove a friend
                    friends.Remove(friend);
                }


Getting Your Friend’s Status

                paras = new TwitterParameters();
                paras.Add(TwitterParameterNames.ID, 36867666);

                TwitterStatusCollection friendsPosts =
                     twit.Status.FriendsTimeline(paras);

                Console.WriteLine("Status For Friends");
                foreach (TwitterStatus status in friendsPosts)
                {
                    if (status.TwitterUser.ScreenName != "petermorano")
                        Console.WriteLine(string.Format("Status For Friend {0}: {1}",
                              status.TwitterUser.ScreenName, status.Text));
                }


Following Someone

                TwitterUser user = new TwitterUser();
                user.ScreenName = "aplusk";
                twit.User.FollowUser(user);


Locating Followers

                TwitterUserCollection followers = twit.User.Followers();
                foreach (TwitterUser follower in followers)
                {
                    Console.WriteLine(follower.ScreenName.ToString());
                    Console.WriteLine(follower.Status.Text.ToString());
                    Console.WriteLine(follower.NumberOfFollowers.ToString());
                }


Handling Errors

The framework also includes a TwitterizerException object that returns exception and request data from the Twitter API. Exceptions can be thrown for a number of reasons, including failure to authenticate, trying to add a friend that is already a friend, and trying to alter someone else's status. The object contains a RequestData child object that contains the details of the original request.

            catch (TwitterizerException tex)
            {
                string message = string.Format("Exception: {0}",
                     tex.Message.ToString());
                Console.WriteLine(message);
            }


Conclusion and Uses

There are a number of ways that an application can leverage the power of Twitter, such as broadcasting changes in a product's price, letting others know that you have posted a new blog, or letting a group of users know that a task is complete. And because statuses can also be read, applications can be find out when another user or application updates their status.

Comments

On 1/28/2010 Hannah said:
hi, i have a problem that there is always an error for the .Status, is there a directive ive not added? thanks in advance

On 1/14/2010 i001962 said:
Thank you for the code samples especially the exception handleing. I am running into an issue and was wondering if you did too ie the scope of the Try Catch objects. If a Try call to TwitterStatusCollection theStatus = t.Status.UserTimeline(parameters2); (e.g.) returns null my susequent processing of the Try clause pukes on reference to the theStatus object which makes sense BUT I dont' know how to get the theStatus object scoped outside the Try clause. Any suggestions?

On 1/8/2010 Diep Duong said:
Thank you very much for very good article ,it would be helpful for .net developers who begin working on Twitterize.However,It would better if we could have an example with People Search and Search Message Content add,two functions above like what Twitter does.

On 1/8/2010 Gaurav Malik said:
Hello Sir/Mam,

On 1/8/2010 sandeepkumar said:
How to twitter followers,

On 1/8/2010 sandeep said:
Remove friends

On 1/8/2010 marvin said:
Hi, Im getting the error message "The remote server returned an error: (401) Unauthorized." when I try to use Status.Update or any method.

On 11/5/2009 Willian said:
Very good post, thanks for your help!

On 9/8/2009 Chris Schnyder said:
I think you'll remove ALL your friends with the code you've got for "Finding Out About Your Friends (and ditching one)"

On 8/22/2009 Dominic said:
Great article, I need this to jump-start my learning with this framework

On 7/27/2009 Jay Nabors said:
I need help, trying to show friends feed through the method you suggested above. I keep getting a "Error Parsing Twitter Response." The stack Trace complains about an invalid token. My code is pretty much a direct VB.NET port of what you wrote in "Getting Your Friend’s Status". Please advise

On 7/21/2009 rakky said:
I have just started using Twitterizer and am having runtime errors communicating with dll. Any ideas?

On 7/1/2009 Costel said:
what is app_source ?

On 7/1/2009 Arvin said:
Hi, for is this may be basic for you but i am geting an error like "error parsing twitter response" in updating and searching status. Need help.

On 5/26/2009 James said:
This is very cool - I am playing with it now and it is easy - does it cover all the possible functions of Twitter?

On 5/20/2009 sandeep said:
how to remove followers,following(friends) of twitter user.

On 5/20/2009 sandeep said:
how to remove followers,following(friends) of twitter user.

On 5/13/2009 gillardg said:
thanks for sharing your knoledge

Leave a Comment

Name:
Email:
URL:
Comment:
Security Code:
Type Security Code:

Photos on Flickr

More Photos »

Search Blog


Get Email Updates

Like what you read here at KeyLimeTie? Sign up for our email list!

Subscribe