Wednesday, April 7, 2010

The Learning Ramp for the Twitter API:Streaming API

The Streaming API is Twitter's third and most advanced API. The other two REST based API's do not give you real time access with its methods, but the streaming API gives you a constant real time flow of data. If you want data from the streaming API you only have to call it once, and the data will update itself as it needs too. Currently only non-protected public statuses are allowed to by used by the streaming API. The Streaming API also filters out low quality users, there is no result relevance all statues are passed through, and the authentication required is basic. There is no rate limit on the streaming API, but you are only allowed to make one connection to it. If you attempt to make multiple connections with the same IP address that can result in being banned from using the Twitter API.  The streaming API only works for json and xml, but Twitter strongly recommends that you use json for the streaming API. There are 5 ways to use the streaming API, and they can be seen here. The streaming API is really good for those apps that need real-time constant updates, for example if you needed up to the date information on a current happening in the world like the earthquake in Haiti.

Friday, April 2, 2010

Learning the Twitter API: OAuth

Anyone using the Twitter API these day seems to be having pain with the OAuth. Well what is OAuth, How does it work, and why does twitter want to switch over to OAuth? Well OAuth is an open form of authorization that allows you to share private resources from one website to another website without having to give your username and password. It works by giving out tokens of data, and each token gives access to a specific site for a specific resource. This way your username and password or not compromised. Think of it like going to a restaurant and giving your credit card to the waiter to charge, but your not giving him your pin number so he can go to an ATM and take all your money. Thats how OAuth works in a nutshell. Twitter wants to switch over to OAuth from Basic Auth because OAuth is much more secure. Twitter is even increasing the rate limit for OAuth users of their twitter api endpoint to 350 call per hour!

Thursday, April 1, 2010

The Learning Ramp for the Twitter API:REST API Methods.

The REST API method allows you to use all the core data from twitter, so the API's here let you do just about anything twitter does. The link is included for information on each of the methods which can be seen here. I will use an example using the user method for returning tweets about a specific user give their user id. To this you need the id of user you want to see. You can find how to find the id number by going here. I will be showing you how use this method to retweet a post from Jamie Oliver(The Naked Chef).

  • URL to use in your browser
    •  http://api.twitter.com/1/users/show.xml?user_id=1401881
  • Valid Formats
    • json
    • xml
  • The HTTP method is GET
  • The id for Jamie Oliver is 18676177
  • So now you simply do the following
    • http://api.twitter.com/1/users/show.xml?user_id=1867177
    • http://api.twitter.com/1/users/show.json?user_id=18676177
If do this in you Firefox web browser you should see the tweets made by Jamie Oliver. Of course you can use this in your app by placing it in your code. I will try and put up an example of this soon.



Wednesday, March 31, 2010

The Learning Ramp for the Twitter API:Search API

The Search API for twitter works by returning what you ask for. To use the search API use the URL in your program, or web browser (I recommend seeing it with the Firefox json viewer): There are actually 5 ways to use the Search API.

  • search
  • trends
  • trends/current
  • trends/daily
  • trends/weekly

Here is an example of how to use the search API. To see how all the others work see Twitter API documentation.
  • http://search.twitter.com/search.format
  • The formats are:
    • JSON
    • Atom
    • For example: http://search.twitter.com/search.json
    • For example: http://search.twitter.com/search.atom
There are a lot of parameters that let you see what type things you want to search for. I can give an example below for me searching for what Apigee tweets about. I will use the q parameter in this example, and use it search from  a user. The example will be in both formats as follows:
  • http://search.twitter.com/search.atom?q=from:apigee
  • http://search.twitter.com/search.json?q=from:apigee
You can place both URL's in your Firefox web browser with the json viewer, and they should both let you see my tweets only. You can also view them using cURL (cURL is a very versatile command line utility which is designed to script web pages interactions.) 

As you can see the Twitter search API is really very simple to use, and can easily be implemented into the app that you are building. Take this very simple python code that uses the twitter API. First you must download the twitter library for python. Here is the code:

import twitter //This is how you call the twitter library.

client = twitter.Api () //You can expose the twitter API with this class
latest_post = client.GetUserTimeline("apigee")// This makes sure the the twitter.API class will fetch Apigee recently posted public tweets.
print [s.text for s in latest_post]//This will print out only the text for Apigee posts.










Tuesday, March 30, 2010

Learning the Twitter API: Some Facts

Here some good facts to know about the Twitter API:
  • Twitter has 2 discrete REST API(s)
    • Search API - Allows developers methods to interact with Twitter Search and trends data.
    • REST API-Allows developers to access core Twitter data like updated timelines, status data, and user information
  • Twitter has 1 Streaming API
    • Streaming API- Provides developers with near real-time high-volume access to Tweets in sampled and filtered form.
  • The Twitter API is RESTful and it supports the following data formats.
    • XML
    • JSON
    • RSS
    • Atom
  • There is a rate limit imposed on all 3 types of APIs.
    • You can only have 150 API requests per hour for the REST API.
    • The Search API has a higher rate limit then 150 requests per hour.
    • The Streaming API has a rate limit appropriate for long lived connections.
  • The Twitter API is HTTP based
    • Methods that receive data from the Twitter API requires a GET request.
    • Methods that submit,change, or destroy data the Twitter API requires a POST request.

Monday, March 29, 2010

Learning Ramp for the Twitter API:Overview of the Twitter API

Twitter bases two of its APIs off the REST architecture so that way the service works better with different Web syndication formats. Twitter works with two Web syndication format known as RSS(Really Simple Syndication) and Atom(Atom Syndication Format), both are ways to receive data from one source and send it to another. Twitter allows third party developers partial access to its API, so people can can easily use Twitter's service in the application that they are building. Twitter has three different types of API's:

  • REST API
  • Search API
  • Streaming API
The REST API and the Search API are the two API's that are based off the REST architecture, while the Streaming API is based of another architecture. In my next few entries I will breakdown the 3 different types of twitter API's, and how they are used.

Learning Ramp for the Twitter API:Terminology

Part the the learning ramp for the twitter API is to make sure I understand all the terminology, and know how it works. Dealing with the twitter API there are a lot of terms that might be unknown to many first time developers, and it is important that everyone know the lingo. In this entry I will go through all the terms I am unfamiliar with and define/explain how they pertain to the twitter API in my own words.


  • API (Application Programming Interface) - To be clear here we are dealing with web API's and they work basically by allowing an interface for the software of two web programs to interact with each other on the back end, and make requests. An analogy could be that you are needing a ride from your Mom to you friend's house because you do not know how to drive, and you have to communicate that to her so she will be to give you a ride to your friend's house. Much like how the the Twitter API works if you have an app that finds only tweets about healthy eating tips you have to allow your app to interact with the Twitter API and ask it to give you only those tweets about healthy eating. 
  • REST (Representational State Transfer) - Is a software architecture that is used for the World Wide Web. REST works by a client requesting some form of information from a server, and that server returning the appropriate response. REST uses sources of specific information, which it calls resources. To alter the resource, servers communicate through a HTTP proxy and exchange representations of the resource. So in non-technical terms for example, I am the client and I want to eat an egg which is the resource, but I the egg (resource) can be represented in many ways such as scrambled,sunny side up, boiled or omelet style from the pan (server).
  • SOAP (Simple Object Access Protocol) - Is a specific protocol for the exchange of structured information in implementation of Web services, so it basically takes a message or API, converts it into machine readable XML format, and thus making that message easier for third party sites to use. 
  • XML ( eXtensible Markup Language) - Is a type of markup language, which converts documents into machine readable format. Basically if you typing some thing in English and sending it to someone who can only read and write in Chinese, by the time you send it the English text is converted into Chinese so the Chinese person can easily decipher the message. XML works in that same way it changes any message you send to your program to machine language so your computer can understand that message as well.
  • JSON (JavaScript Object Notation) -Is an alternative to XML, but works in the same way. It works by serializing and transmitting structured data between a server and a web application. The difference is that JSON is in human readable format, and it is only used for data exchange. It is primarily used in data exchange over the internet. JSON is also much less complex and smaller in size than XML thus making it lightweight, but both struggle with mechanisms for representing large binary data types like image data.
So what I learned from all of these terms is that REST is much more flexible in data exchange then SOAP, which is why I assume that twitter's API is RESTful. Also I learned that JSON is the preferred way to exchange data over the internet, because it is simpler and more lightweight then converting the data to XML. It makes sense why people don't like using SOAP!