ocial networking has reached critical mass. One unique social networking platform, Twitter, launched in March of 2006 and took the world by storm with its social networking and microblogging platform.
The developers of Twitter had the forethought to provide a REST-based API. Numerous developers have used the API to build Twitter clients on dozens of different platforms. In this article I'll demonstrate how to access Twitter using the .NET platform.
Twitter provides a unique communications platform. I like to think of it as a massively multi-player text messaging platform. The basic concept has three main actors: statuses
(people who follow other people) and followees
(people who are followed). Data basically flows like this: people post statuses, which are persisted into the sender's Twitter feed and replicated into their followers' Twitter feeds.
Anatomy of a URL
The Twitter API provides a nicely structured and highly flexible set of mechanisms for accessing the Twitter platform. You access data from Twitter by calling specific URLS which Twitter processes and then Twitter returns your data in a number of standardized formats including XML, RSS, ATOM and JSON.
The Twitter URL API consists of three main parts: request, return type, and optional parameters. Examine the following URL:
portion of the message specifies the type of message (action) to process. The example message returns the timeline (last set of statuses) for a specified user.
The second part of the request is the basic parameter of the request. The sample parameter, rodpaddock
, tells the statuses/user_timeline
request to return my timeline.
The final parameter, .xml
specifies the data format I want Twitter to return—XML format, in this case. Alternatively, to get Twitter to return a different format, I could change the extension to .rss
, or .atom
demonstrates the data returned from the example call.
|Figure 1. Status XML: Here's the XML Twitter returns for the sample request.|
Most Twitter REST calls also support a number of optional parameters. The user_timeline
request has the following optional parameters:
- id: returns messages using the posters id
- count: returns specified number of messages (default is 20, maximum is 200)
- since: returns messages since a specified date
- since-id: returns messages since a specified message id
- page: returns messages from a specified display page
When passing optional parameters to Twitter you do it just like you would when passing parameters to a web request via the URL—by placing the parameters after a question mark. As an example, the following call returns the last 200 messages from my timeline:
|Social networking is here to stay. One of the most popular social networking and microblogging platforms is Twitter. Twitter really took off after winning a web award at SXSW (South by Southwest) in 2007, and had phenomenal growth in 2008. According to mashable.com, in 2008 Twitter went from 500,000 to 4,430,000 unique monthly visitors.|
The following URL returns my last 200 statuses from December 1, 2008.
Twitter returns self-contained XML, meaning you don't need to make follow up calls when dealing with XML return data. A status XML has this basic structure:
Each status record contains all the relevant data, including the details of its creator. It doesn't get much simpler than that.
|Editor's Note: This article was first published in the March/April 2009 issue of CoDe Magazine, and is reprinted here by permission.|