Twitter have made data retrieval pretty damn simple.. I managed to lookup all the info I needed and write this code in just a few minutes.. My requirements were quite simple though, all I had to do was retrieve the top 2 latest tweets from a specific user and display them on a page.
All the documentation you need can be found here: https://dev.twitter.com/docs. To fulfill this requirement I needed to use the following api url: https://api.twitter.com/1/statuses/user_timeline.json. This allows me to retrieve tweets from a user.
Let’s get started..
The parameters i’ll pass to the service are:
screen_name=evil_jared – the user I’m retrieving the tweets from
count=2 – the max number of items to return
trim_user=true – don’t include any user specific info in the response.. The smaller the response, the better..
Open up your browser and point to https://dev.twitter.com/console, copy the following url and hit “get”: https://api.twitter.com/1/statuses/user_timeline.json?screen_name=evil_jared&count=2&trim_user=true
This will return a nicely formatted json response, great.. Now to retrieve this data via a jQuery ajax call:
First, you’ll need to reference the latest version of jQuery.
Create a function, which will retrieve the info using a $.getJSON request and alert the top 2 tweets to the user:
//You’ll want to do something a little more useful with the data, such as writing it to the page.
Don’t forget to include the callback (callback=?) parameter in the url! Without this, you will not get a response. Including the callback parameter invokes a JSONP request instead, allowing you to make crossdomain requests. More info here: http://api.jquery.com/jQuery.getJSON/
That’s it. To get more info about what variables to pass the api: https://dev.twitter.com/docs/api/1/get/statuses/user_timeline
To view a quick demo + src, take a look here: http://www.applogix.co.za/blogs/twitter%20jquery/index.html