Home > Code Samples, Coding > Twitter API with C# example(part 2)

Twitter API with C# example(part 2)

sundial

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:

  |  copy code |? 
01
public string FetchFollowers(string userName, string password)
02
{
03
    using (var client = new WebClient())
04
    {
05
        client.Credentials = new NetworkCredential(userName, password);
06
        try
07
        {
08
            using (var stream = client.OpenRead("http://twitter.com/statuses/followers.xml"))
09
            {
10
                using (var reader = new StreamReader(stream))
11
                {
12
                    return reader.ReadToEnd();
13
                }
14
            }
15
        }
16
        catch (WebException ex)
17
        {
18
            //insert code to deal with exception HERE               
19
        }
20
    }
21
    return string.Empty;
22
}

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:

  |  copy code |? 
1
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

  1. No comments yet.
  1. No trackbacks yet.

Spam Protection by WP-SpamFree