Using Twitter API 1.1 with Twitterizer for C#

So, yesterday (June 11, 2013) we published an minor aesthetic update to our website. Shortly after publishing the update, we noticed the twitter feed on our homepage wasn’t working. We initially thought it was caused by the update, but it turns out that Twitter’s API 1.0 was ‘retired’ and version 1.1 of the API was introduced.

The new API requires that all requests be authorized (via OAuth) and served over HTTPS. This is similar to, but still more restrictive than, Facebook’s API because not even basic requests for public information can be done without authentication.

The Solution: Twitterizer

First of all, I’d like to say Twitter’s API documentation (and Facebook’s for that matter,) is rather poor. It does attempt to explain different methods that are available but we’re going to focus on the using the Twitterizer method which is a C# library that handles much of the OAUth stuff for you. We chose it because we preferred to use C# in this case instead of some of the other alternatives languages. It is important to note that the Twitterizer example on the API documentation page will not work with the 1.1 (it’s for 1.0). That said, the developer smartly included some hooks to allow it to work with 1.1.

Implementing Twitterizer in 4 Steps

  1. Download Twitterizer (Visual Studio required). Open the Solution and build the main “Twitterizer2″ project. It will produce two dll’s:
    • Twitterizer2.dll
    • Newtonsoft.Json.dll
  2. Reference Twitterizer2.dll and Newtonsoft.Json.dll in your project.
  3. Go to dev.twitter.com and sign in with your twitter account. Once logged in, click the Me button (where sign up used to be) and click My Applications. Create a new application for your website (or whatever project will be consuming the Twitter data,)  if one does not already exist. Among other information, it will provide you with the following details you will need for authentication:
    • Consumer Key
    • Consumer Key Secret
    • Access Token
    • Access Token Secret
  4. In your project, enter the following code to retrieve 20 of your most recent Tweets in JSON:
    OAuthTokens tokens = new OAuthTokens();
    tokens.ConsumerKey = "ConsumerKey"; //<-- replace with yours
    tokens.ConsumerSecret = "ConsumerKeySecret";//<-- replace with yours
    tokens.AccessToken = "AccessToken";//<-- replace with yours
    tokens.AccessTokenSecret = "AccessTokenSecret";//<-- replace with yours
    
    //USER TIMELINE (ALL TWEETS)
    UserTimelineOptions userOptions = new UserTimelineOptions();
    userOptions.APIBaseAddress = "https://api.twitter.com/1.1/"; // <-- needed for API 1.1
    userOptions.Count = 20;
    userOptions.UseSSL = true; // <-- needed for API 1.1
    userOptions.ScreenName = "Your Twitter Screen/Username";//<-- replace with yours
    TwitterResponse<TwitterStatusCollection> timeline = TwitterTimeline.UserTimeline(tokens, userOptions);

    IMPORTANT! Setting the UseSSL and APIBaseAddress properties to match the values in the example code above is what actually instructs Twitterizer to use API 1.1.

    Note that you must replace the token value’s with your own values you got from step #3, and specify your screen/username. Inspecting the timline.ResponseObject  property, we can see our 20 tweets in a JSON array. We can now pass that JSON to the front-end via a model (if you’re using an MVC solution), or however meets your needs.

  5. It is also important to note that there are a plethora of methods available in Twitterizer that you can use to manipulate all sorts of data from the API but all of that is outside the scope of this post. Hopefully this helps you get up and running quickly with version 1.1 of API.
About these ads

18 thoughts on “Using Twitter API 1.1 with Twitterizer for C#

  1. Hi,
    i want to follow user with twitterizer but i have some problems.I use this module but i can’t get “listId” and “OptionalProperties options” values.

    TwitterResponse AddMember(OAuthTokens tokens, string ownerUsername, string listId, decimal userIdToAdd, OptionalProperties options)

    Thanks,

  2. Pingback: Using Twitter API 1.1 with Twitterizer for C# | dotnetqueries

  3. -45.56142506
    -46.39093137
    0
    0
    0
    2013-08-07T13-33
    08
    07
    2013
    33
    13
    8/7/2013
    1:33 PM
    637.1
    B

    you need DLLS for api1.1 from this site https://bitbucket.org/szrsharp/twitterizer-remaked-for-api-1.1/overview
    (**OR**)
    get the Twitterizer source ( https://github.com/Twitterizer/Twitterizer), and change Twitterizer/Twitterizer2/Core/OptionalProperties.cs file. Specifically this line:
    this.APIBaseAddress = “http://api.twitter.com/1/”; change to /1.1/ and recompile.

    The twitterizer website leads you to believe it is updated to used API 1.1 but it is NOT!! I wasted over a day debugging this.

    • Hello Johnny,

      You are correct. However, as explained (via comments) in the example code, making use of the UseSSL (set to true) and APIBaseAddress (set to “https://api.twitter.com/1.1/”) properties, is a way to get the existing Twitterizer to work with API 1.1. I’ll update the post to make it a little clearer.

  4. This definitely got me much more comfortable and have a better understanding of how to use this library. Thank you very much for the tutorial!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s