Processing Twitter and New York Times APIs with ASP.NET Ajax on Microsoft CDN

6 min read

APIs (Application Programming Interface) are application-to-application programming interfaces that support harvesting information on the web using the known web standards. These APIs are provided by the entities who wish to expose parts of their resources that a third party can use. The APIs run transparent to the user and exposes just what they want to expose, with some providing access to material for public consumption with others giving access to resources based on authentication. In a sense they may be called a basic form of SAAS., Google etc have exposed their APIs for some time. Twitter and New York Times have also exposed their API’s which can be used to do some digging into the information contained in them, a kind of web mining. Many others such as Netflix have provided their own APIs described on their web sites.

What is Twitter API?

Twitter API is provided by the Social Networking and Micro-blogging service. Twitter API adheres to the web standards and one can talk to Twitter using HTTP. You can just about access anything on the Twitter web site. One example of creating a Microsoft SQL Server Report using Twitter API is available here – Tweets with Reporting Services, wherein the response from the Twitter API was in XML format. JSON (JavaScript Object Notation) is another format in which data is returned when an API call is made. In this article we will be looking at API call that returns a JSON response.

Twitter exposes a large number methods through their API’s such as API’s for Search, Timeline, Status, User, Direct Message, Friendship and many more. As previously mentioned the responses will be in XML or JSON. Also while some APIs may take parameters others may not.

The Twitter API used in this tutorial

We will be looking at trends in Twitter API exposed by the url, We will be using the GET method and we will expect a JSON response. Since the volume of traffic may overwhelm, the calls that you can make to this in an hour are limited (also known as rate limiting) but not critical for the demo in this tutorial.

Here is a typical call to the trends method on the Twitter API. Herein we will search for trends on the Twitter site and expect a response in JSON, if we use json instead of Format in the next URL address.

Instead of:
type-in, the following for URL address,

When you plug the above in a web Brower you would get a response trends.json which you may save to your hard drive or, use it in any way you like. The next quoted text is what you get in response (note that this is what I got on Saturday 31, 2009 and what you get will be different), the content of the file trends.json you saved to your computer. Note that presently you get about top ten trends from this API call.

{"as_of":"Sat, 31 Oct 2009 20:44:46 +0000","trends":[{"name":"Happy Halloween", 
search?q =%23potterday"},{"name":"X Factor","url":"
search?q=%22X+ Factor%22"},{"name":"It's Halloween","url":"http://search.twitter.
com/search?q=%22It %27s+Halloween%22+OR+%22Its+Halloween%22"},{"name":"Trick",
{"name":"This Is It","url":""}]}

First of all what you see returned is a JSON object. If you are new to JSON review this article on my blog. The various elements that you see such as ‘name’, ‘url’ etc are fields in the response that are all described in the API documentation(look for Return Values). Some of the API calls can return a ton of information and you will have to know the API method so that you can correctly parse this data. Another thing you would notice is that the JSON object you get out is a nested object with many levels. You may need a JSON Parser to get a clearer picture of this nesting and I recommend using the online parser at this site. Using the above site, the JSON Object would appear as shown (only a portion is shown).

Microsoft AJAX and jQuery

New York Times API

New York Times made available to the developers sometime in the middle of October 2008 APIs that can search New York Times for various kinds of information . Just like in Twitter there are a large number of APIs that you can use such as: Article Search; Best Sellers; Campaign Finance; Congress; and many others. Interested users can get on to this resource by signing up here requesting what APIs they would like to use. After signing up, New York Times would provide keys for the APIs that you want to access. It is important therefore, that the call should include the keys provided to you. For example, I received keys to access the following resources: Movie Reviews, Article Search, Best Sellers and Times Newswire. The key for the Movies Reviews API appears as shown here (the one shown here has been doctored and will not work).

Movie Reviews API	 
Key: b57378910b9fd80ecc73461547c93e8a:10:50673441

Using the New York Times API

It is a valuable resource since you can get for example with the Article Search API access to more than 2.8 million articles from 1981. Using this is quite simple, just paste the URL shown below into the address box of your browser. Note that the key shown here is fake (but of correct format).

This will display in the browser the JSON object that is returned as shown. You can use the previously mentioned site to parse it for more friendly display.

{"facets" : {"publication_year" : [{"count" : 2724 , "term" : "2008"} , 
{"count" : 2345 , "term" : "2006"} , {"count" : 2311 , "term" : "2009"} ,
{"count" : 2282 , "term" : "2007"} , {"count" : 2144 , "term" : "2002"} ,
{"count" : 2111 , "term" : "2001"} , {"count" : 1988 , "term" : "2005"} ,
{"count" : 1951 , "term" : "2004"} , {"count" : 1921 , "term" : "1985"} ,
{"count" : 1798 , "term" : "2003"} , {"count" : 1761 , "term" : "1999"} ,
{"count" : 1720 , "term" : "2000"} , {"count" : 1642 , "term" : "1998"} ,
{"count" : 1442 , "term" : "1984"} , {"count" : 1382 , "term" : "1986"}]} ,
"offset" : "0" , "results" :
[{"body" : "BARSUR, India — At the edge of the Indravati River, hundreds
of miles from the nearest international border, India effectively ends.
Indian paramilitary officers point machine guns across the water.
The dense jungles and mountains on the other side belong to Maoist rebels
dedicated to overthrowing the government. "That is their liberated" ,
"byline" : "By JIM YARDLEY" , "date" : "20091101" , "title" :
"Maoist Rebels Widen Deadly Reach Across India" , "url" : "http://www.
/01maoist.html"} ,.........(there is more of this but abbreviated here)

Response Format

As you can see the responses to the API calls return JSON objects in general of the form shown belo

w (this one is of the form returned by the Twiiter API). What we propose to do is to use jQuery’s GetJSON() method to get the JSON Objects and use Microsoft AJAX JavaScript files to display the data on the web page. Both jQuery javascript files and Microsoft ASP.NET AJAX files are both available on the Microsoft ECN (CDN). The GetJSON() method as well as the Microsoft ASP.NET AJAX templates can be easily implemented in the Visual Studio 2008 IDE. Alternatively Microsoft AJAX can also be used to retrieve data from the web sites. In this article the GetJSON() method will be used.

{"x":{"y":[{"a1":"b1", "c1":"d1"}, {"a2":"b2", "c2":"d2"}]},....


Please enter your comment!
Please enter your name here