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
- Download Twitterizer (Visual Studio required). Open the Solution and build the main “Twitterizer2” project. It will produce two dll’s:
- Reference Twitterizer2.dll and Newtonsoft.Json.dll in your project.
- 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
- 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.
- 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.