This article was previously published in my blog, Just Like a Magic.
Overview
This writing discusses the Twitter API and how you can utilize it in your managed application. It begins by a brief discussion of the API and the methods used. After that, it digs into the discussion of how you can utilize the API into your application with help of code samples and examples. At the end of this writing, there's a nice open-source Twitter client application available for download. Worth mentioning that this article focuses on the REST API set of Twitter, specifically XML endpoints.)
Introduction
Today, we are talking about Twitter as it is one of the most famous social networking services. This article discusses how you can create Twitter clients using .NET Framework. Oh, before we start, I'd like to introduce you my twitter account first J, it's @elsheimy.
Twitter API
What Twitter API is
Before we dig into the discussion of Twitter API we need to know first what the Twitter API is.
Twitter API is a set of endpoints (or methods) that allow applications to work with Twitter data.
Twitter API consists of three parts:
- Two REST APIs:
- REST API:
This is the core API set; it allows developers to access core Twitter data, it contains most (if not all) of the methods and functions you would use to utilize Twitter data in your application, and it supports (mostly) three formats (or endpoints) for each method: XML, Atom, and JSON formats. - Search API:
Fully integrating search methods allows you to search for tweets based on specific criteria and to search for daily/weekly hot trends. It supports only the JSON format. This set of API was originally developed by Summize, Inc. and integrated into the Twitter API. (Twitter is looking forward to unify the REST API.)
- REST API:
- Streaming API:
A set of API methods that allow near-realtime access to various subsets of Twitter public statuses. All methods are in JSON format, no XML or ATOM endpoints available.
Actually, the REST API would be very sufficient for you unless you find it easier to handle the JSON format.
It is worth mentioning that the REST API provides very handful and sufficient search methods that your application can use. If you need more searching capabilities (like searching for tweets that confirm to a set of criteria,) you can dig into the REST Search API.
You are not required to stick to a specific set of API methods. You can mix the methods you like from the three APIs.
Wikipedia: REST (Representational State Transfer) is a style of software architecture for distributed hypermedia systems such as the World Wide Web.
Wikipedia: JSON (JavaScript Object Notation) is a lightweight text-based open standard designed for human-readable data interchange based on the JavaScript programming language.
Wikipedia: Atom Publishing Protocol (AtomPub or APP) is a simple HTTP-based protocol for creating and updating web resources.
This writing focuses on the XML endpoints of the REST API.
API Documentation
Twitter has with a very nice API documentation that documents each and all methods and endpoints of the three parts of the API. This documentation can be found here: http://apiwiki.twitter.com.
To avoid duplication, we are not going to discuss the Twitter API again in this writing; it is already documented and available for all users. Instead, we will take a brief look at the API and discuss how we can utilize it in our .NET application. For simplicity, we will focus on XML endpoints. Therefore, all of our code would rely on the XML features of .NET Framework 2.0 (i.e. System.Xml.dll library.) Oh, you are free to write the code the way you like (e.g. integrating LINQ for XML into the code.)
Now, we are going to look inside the Twitter API.
Things to be Kept into Mind
There are some basics of the Twitter API that a developer should keep into his mind.
- Calls to the Twitter API are limited:
Don't expect to post unlimited updates or to follow thousands of users in just an hour! Many methods have a call limit (hourly/daily.) For example, at the time of this writing, you have only 150 API requests per hour, further requests would return an exception. In addition, you can send only 1,000 updates and 250 direct messages per day (can we remove this word "only"?) You can check Twitter limits here. - Every endpoint has its HTTP methods that need to be set (e.g. GET, POST, and DELETE) in the request. The documentation of each endpoint lists the HTTP methods required for that endpoint. For more information about HTTP and HTTP methods check the RFC 2616 Section 9 document.
- Some methods (like methods send updates) require user authentication and others not. Authentication is discussed soon.
API Methods
The following is a discussion about Twitter API methods and how you can call them.
Method Address
Every method has an address (i.e. URI). The address of a method is something like that: http://api.twitter.com/1/statuses/public_timeline.format (for the statuses/public timeline method.) We replace the format field with the format we like to work with (e.g. xml, atom, json.)
Now, try to copy the method address http://api.twitter.com/1/statuses/public_timeline.xml and paste it into your browser. You should get something like that:
<?xml
version="1.0" encoding="UTF-8"?>
<statuses
type="array">
<status>
<created_at>Wed Apr 14 19:32:07
+0000 2010</created_at>
<id>12179999634</id>
<text>35 Ingenious Examples of
Footwear - http://su.pr/1JxMAr</text>
<source><a
href="http://apiwiki.twitter.com/" emp_href="http://apiwiki.twitter.com/"
rel="nofollow">API</a></source>
<truncated>false</truncated>
<in_reply_to_status_id></in_reply_to_status_id>
<in_reply_to_user_id></in_reply_to_user_id>
<favorited>false</favorited>
<in_reply_to_screen_name></in_reply_to_screen_name>
<user>
<id>15736190</id>
<name>Smashing
Magazine</name>
<screen_name>smashingmag</screen_name>
<location>Freiburg,
Germany</location>
<description>Vitaly Friedman,
editor-in-chief of SmashingMagazine.com ...</description>
<profile_image_url>http://a3.twimg.com/profile_images/572829723/...</profile_image_url>
<url>http://www.smashingmagazine.com</url>
<protected>false</protected>
<followers_count>166019</followers_count>
<profile_background_color>B2DFDA</profile_background_color>
<profile_text_color>333333</profile_text_color>
<profile_link_color>f51616</profile_link_color>
<profile_sidebar_fill_color>ffffff</profile_sidebar_fill_color>
<profile_sidebar_border_color>eeeeee</profile_sidebar_border_color>
<friends_count>394</friends_count>
<created_at>Tue Aug 05
14:00:40 +0000 2008</created_at>
<favourites_count>4</favourites_count>
<utc_offset>-10800</utc_offset>
<time_zone>Greenland</time_zone>
<profile_background_image_url>http://s.twimg.com/a/...</profile_background_image_url> <profile_background_tile>true</profile_background_tile>
<notifications>false</notifications>
<geo_enabled>true</geo_enabled>
<verified>false</verified>
<following>false</following>
<statuses_count>8703</statuses_count>
<lang>en</lang>
<contributors_enabled>false</contributors_enabled>
</user>
<geo />
<coordinates />
<place />
<contributors />
</status>
. . .
</statuses>
This method returns the 20 most recent statuses from arbitrarily-selected non-protected Twitter users; this list is called the Public Timeline. Because we have selected the XML format (or endpoint) we end up with XML data.
From the data returned we can extract the structure of statuses and users. Twitter API is good enough; it uses the same structure for all data returned. In other words, all methods (in the REST API, remember?) use the same structure (i.e. schema) to represent status and user objects. Those structures are covered soon.
Methods with Arguments
Most of methods accept arguments, some are optional and others are required. An example of a function requires a single argument is the users/show method that returns profile information about a specific user.
This function accepts one of three arguments:
- id:
The ID or screen name of the user. - user_id:
The ID of the user. - screen_name:
The screen name of the user.
We set arguments the same way we set web pages query strings. Considering my twitter account @elsheimy as an example, we could query user profile using one of four ways:
- http://api.twitter.com/1/users/show.xml?id=19645411
- http://api.twitter.com/1/users/show.xml?id=elsheimy
- http://api.twitter.com/1/users/show.xml?user_id=19645411
- http://api.twitter.com/1/users/show.xml?screen_name=elsheimy
All of the four calls return the same results that should be like this:
<?xml version="1.0" encoding="UTF-8"?> <user> <id>19645411</id> <name>Mohammad Elsheimy</name> <screen_name>Elsheimy</screen_name> <location>KB, Egypt</location> <description>Technology evangelist from Egypt born in 1991</description> <profile_image_url>http://a1.twimg.com/profile_images/833061500/...</profile_image_url> <url>http://JustLikeAMagic.WordPress.com</url> <protected>false</protected> <followers_count>278</followers_count> <profile_background_color>DBE9ED</profile_background_color> <profile_text_color>333333</profile_text_color> <profile_link_color>CC3366</profile_link_color> <profile_sidebar_fill_color>E6F6F9</profile_sidebar_fill_color> <profile_sidebar_border_color>DBE9ED</profile_sidebar_border_color> <friends_count>179</friends_count> <created_at>Wed Jan 28 10:47:36 +0000 2009</created_at> <favourites_count>7</favourites_count> <utc_offset>7200</utc_offset> <time_zone>Cairo</time_zone> <profile_background_image_url>http://s.twimg.com/a/1271811071/...if</profile_background_image_url> <profile_background_tile>false</profile_background_tile> <notifications></notifications> <geo_enabled>true</geo_enabled> <verified>false</verified> <following></following> <statuses_count>1877</statuses_count> <lang>en</lang> <contributors_enabled>false</contributors_enabled> <status> <created_at>Wed Apr 21 16:21:46 +0000 2010</created_at> <id>12585240751</id> <text>I'm reading this, it's really hot: #RFC 2616 - Hypertext Transfer Protocol ...</text> <source><a href="http://bit.ly" rel="nofollow">bit.ly</a></source> <truncated>false</truncated> <in_reply_to_status_id></in_reply_to_status_id> <in_reply_to_user_id></in_reply_to_user_id> <favorited>false</favorited> <in_reply_to_screen_name></in_reply_to_screen_name> <geo/> <coordinates/> <place/> <contributors/> </status> </user>
As you can see, it returns user information and the last update of that user. Notice that the structure of the data is the same in all methods.
Methods Require Authentication
Some methods require user authentication. Examples are functions update status, send direct messages, retrieve friends' timeline, etc.
Let's take an example. Bring up your browser and browse to the address of statuses/friends timeline method that returns user's friends' timeline, http://api.twitter.com/1/statuses/friends_timeline.xml. A small window like this should appear that asks you your Twitter username and password.

Figure 1. IE Authentication
Provide your Twitter username and password to pass to the results. If authentication was OK, you should receive the most recent 20 statuses of your friend timeline in the same structure (schema) as the Public Timeline method. On the other hand, if the authentication process failed, you should receive an error:
<?xml version="1.0" encoding="UTF-8"?>
<hash>
<request>/1/statuses/friends_timeline.xml</request>
<error>Could not authenticate you.</error>
</hash>Let's consider another example, the direct_messages/new method. This function sends a new direct message to a specific user from the current user authenticated.
This function accepts two required arguments:
- user:
The ID or screen name of the recipient user. You can use one of two arguments in place of this argument:- screen_name:
Screen name of the user. - user_id:
The ID of the user.
- screen_name:
- text:
Message body, not longer than 140 UTF-8-encoded characters.
The following two calls send the same message ("hi, how is it going") to the same user:
- http://api.twitter.com/1/direct_messages/new.xml?user_id=19645411&text=hi,%20how%20is%20it%20going
- http://api.twitter.com/1/direct_messages/new.xml?screen_name=elsheimy&text=hi,%20how%20is%20it%20going
Notice that we need to encode arguments before we pass them to the server.
Because this function updates data, it requires HTTP POST method. Therefore, you won't be able to try it from your browser unless your browser allows you to set HTTP methods in the request.
If the function succeeded, it should return the message object that has been sent, and it should be like this:
<?xml version="1.0" encoding="UTF-8"?> <direct_message> <id>88619848</id> <sender_id>1401881</sender_id> <text>all your bases are belong to us.</text> <recipient_id>7004322</recipient_id> <created_at>Wed Apr 08 20:30:04 +0000 2009</created_at> <sender_screen_name>dougw</sender_screen_name> <recipient_screen_name>igudo</recipient_screen_name> <sender> <id>1401881</id> <name>Doug Williams</name> <screen_name>dougw</screen_name> <location>San Francisco, CA</location> <description>Twitter API Support. Internet, greed, users, dougw ...</description> <profile_image_url>http://s3.amazonaws.com/twitter_production/...</profile_image_url> <url>http://www.igudo.com</url> <protected>false</protected> <followers_count>1036</followers_count> <profile_background_color>9ae4e8</profile_background_color> <profile_text_color>000000</profile_text_color> <profile_link_color>0000ff</profile_link_color> <profile_sidebar_fill_color>e0ff92</profile_sidebar_fill_color> <profile_sidebar_border_color>87bc44</profile_sidebar_border_color> <friends_count>290</friends_count> <created_at>Sun Mar 18 06:42:26 +0000 2007</created_at> <favourites_count>0</favourites_count> <utc_offset>-18000</utc_offset> <time_zone>Eastern Time (US & Canada)</time_zone> <profile_background_image_url>http://s3.amazonaws.com/...</profile_background_image_url> <profile_background_tile>false</profile_background_tile> <statuses_count>3394</statuses_count> <notifications>false</notifications> <following>false</following> <verified>true</verified> </sender> <recipient> <id>7004322</id> <name>Doug Williams</name> <screen_name>igudo</screen_name> <location>North Carolina</location> <description>A character.</description> <profile_image_url>http://s3.amazonaws.com/...</profile_image_url> <url>http://www.igudo.com</url> <protected>false</protected> <followers_count>19</followers_count> <profile_background_color>69A1AA</profile_background_color> <profile_text_color>000000</profile_text_color> <profile_link_color>F00</profile_link_color> <profile_sidebar_fill_color>ACBEC1</profile_sidebar_fill_color> <profile_sidebar_border_color>8A8F85</profile_sidebar_border_color> <friends_count>3</friends_count> <created_at>Thu Jun 21 21:16:21 +0000 2007</created_at> <favourites_count>0</favourites_count> <utc_offset>-18000</utc_offset> <time_zone>Eastern Time (US & Canada)</time_zone> <profile_background_image_url>http://static.twitter.com/...</profile_background_image_url> <profile_background_tile>false</profile_background_tile> <statuses_count>382</statuses_count> <notifications>false</notifications> <following>true</following> <verified>true</verified> </recipient> </direct_message>








Vikash KumarPosted Aug 5, 2016, 1:53 AM
Getting error for Api_UrlShortner