Twitter From ASP.NET – Friends vs Followers

F03I0051 If you want to emulate the services of something like Twitter Karma you are going to need to learn how to retrieve your list of friends and followers.

What makes this especially difficult is that the documentation for this on both the Twitterizer site (where it is practically non-existent) and on the Twitter site (where it is not exactly accurate) makes figuring out this process particularly difficult.

The two main elements you need to be aware of prior to writing the code to retrieve your list of friends or followers is that you can only retrieve 100 friends or followers at a time and that you need to specify which page of followers you are looking for by passing a parameter.  Hopefully just knowing this will save you the hours of searching I had to do to find this information.

One other thing that might be useful when you have a lot of friends or followers is this final bit of information.  You can only make 100 API calls an hour.  This means that you can only retrieve 100 * 100 (10,000) friends or followers at a time and you’ll need to get a waiver from Twitter so you can make more API requests.

So, on to the code.  I’m going to concentrate on retrieving friends.  The followers code is similar.

As I mentioned, you’ll need to pass a parameter.  All parameters in Twitterizer are objects of type TwitterParameter.  We want to create a Twitter parameter that holds the page number.  The code for that looks like this:

TwitterParameters param = new TwitterParameters();
param.Add(TwitterParameterNames.Page, page);

where page is an integer starting at the number one.

Then we call the method to return that page of 100 friends

TwitterUserCollection fc = twit.User.Friends(param);

where twit is a Twitter object created by passing in the username and password as we’ve done in previous posts about twitter.

The rest of your code will then do something with that information by retrieving each TwitterUser object from the collection and doing something with it, like putting it in a database.

foreach (TwitterUser u in fc)
{
    // Do something with the TwitterUser here
}

If you are storing the user information, I’d use the TwitterUser.ID rather than the TwitterUser.UserName or TwitterUser.ScreenName.  Both the UserName and ScreenName can and probably will change for an account, but the ID will remain constant.

Related Post

Leave a Reply

Comment Policy:

  • You must verify your comment by responding to the automated email that is sent to your email address. Unverified comments will never show.Leave a good comment that adds to the conversation and I'll leave your link in.
  • Leave me pure spam and I'll delete it.
  • Leave a general comment and I'll remove the link but keep the comment.

Notify me of followup comments via e-mail

Bear