Getting Twitter Profile Image Via C# With 1.1 API

May 1 2013

If you know someone's Twitter handle and would like to display their twitter avatar on your website, here's some code to get the URL of their avatar.  First, you'll need to register at and acquire a ConsumerKey, ConsumerSecret, Token and TokenSecret -- now that Twitter supports application only authentication, there isn't any handshaking involved; you just need to craft up the right OAuthCredentials for a ProtectedResource, which the Hammock library does for you. Love that library: you can get it here: or grab it as a NuGet package:

Here’s the code; nothing too fancy:

using System;
using Hammock;
using Hammock.Authentication.OAuth;
using Hammock.Web;
using Newtonsoft.Json.Linq;

namespace ExternalServices
    public class TwitterAvatarLookup : ITwitterAvatarLookup
        const string ConsumerKey = "";
        const string ConsumerSecret = "";
        const string Token = "";
        const string TokenSecret = "";

        public string GetTwitterAvatarUrl(string twitterHandle)
            string avatarUrl = string.Empty;
            var request = new RestRequest
                Credentials = new OAuthCredentials
                    Type = OAuthType.ProtectedResource,
                    SignatureMethod = OAuthSignatureMethod.HmacSha1,
                    ParameterHandling = OAuthParameterHandling.HttpAuthorizationHeader,
                    ConsumerKey = ConsumerKey,
                    ConsumerSecret = ConsumerSecret,
                    Token = Token,
                    TokenSecret = TokenSecret,
            request.Path =

            request.Method = WebMethod.Get;
            RestClient client = new RestClient();
                RestResponse response = client.Request(request);
                JArray jArray = JArray.Parse(response.Content);
                avatarUrl = (string)jArray[0]["profile_image_url_https"];

            catch (Exception)
                return "default.png";
            return avatarUrl;


If you know more than one handle whose avatar you need to get, the API supports passing multiple user handles; see

Skip, Take and Twitter Paging: Using TweetSharp To Download All Tweets from the Twitter Timeline

February 4 2010

I have been using TweetSharp lately and I am pretty happy with it so far. One thing that was a little confusing is how they implemented paging in their API on top of the Twitter API. 

What they did was to use the LINQ syntax (Skip and Take). Basically, think of Skip as the page number and Take as the number of records on a page. So, let's say you want to download all of a person's tweets. This assumes of course that you've already gone through the OAuth process and have permission to do so.  Twitter lets you download a maximum of 3200 tweets from a user, 200 tweets at a time.

So, the code to grab all of a user's tweets using TweetSharp would be as follows:

            for (int i = 1; i < 17; i++)
                var query = FluentTwitter.CreateRequest()
                var responses = query.Request().AsStatuses();
                if (responses.Count() > 0)
		    //do something with tweets

 So, what's going on here?  Basically I am attempting to download all 3200 tweets with a for loop, asking to take 200 tweets for each page, which is my counter. If I get less than 3200, I exit the loop, thus not unnecessarily calling the Twitter API (and increasing my chances of getting rate limited).



The Twitter API: The Schrodinger's Cat of Web Service APIs

January 25 2010

I just started reading Professional Twitter Development: With Examples in .NET 3.5 (Wrox Programmer to Programmer) and came across this quote, which cracked me up:

The Twitter API is the Schrödinger’s cat of web service APIs. Until you call it, you never know if it's alive or dead. Sometimes the mere act of calling it is enough to kill it.
--Scott Koon,Witty

Hilarious. And true.