Twitter API with C# example(part 2)

First of all, I have to admit that I decided to take a vacation. Vacation from everything – work, freelance, projects, blog, etc. It’s summer and despite the crappy weather here in Toronto, I am spending time with my family which actually rocks. Now vacation is over, let’s get back to coding – today I wanted to continue with Twitter examples. Twitter seems to be very hot, and I was curious as to how to get the most out of it from the coding perspective. Here is a tiny function that gets the followers of a user, providing we know the username and password:
public string FetchFollowers(string userName, string password)
{
using (var client = new WebClient())
{
client.Credentials = new NetworkCredential(userName, password);
try
{
using (var stream = client.OpenRead("http://twitter.com/statuses/followers.xml"))
{
using (var reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
}
catch (WebException ex)
{
//insert code to deal with exception HERE
}
}
return string.Empty;
}
There are two types of “connection” between the people on twitter – followers and friends – I am too lazy to look up the difference between the two, but if you want list of friends then line 8 in the function above should look like this:
using (var stream = client.OpenRead("http://twitter.com/statuses/friends.xml"));
I usually stuff the output of that in XML and then do what I need to do with it.
Let me take a break for now, next time we will cover posting to Twitter (status update).
BTW, first part of this could be found here
