Click here to Skip to main content
15,867,686 members
Articles / Desktop Programming / Windows Forms

Creating a Simple Twitter Client Application

Rate me:
Please Sign up or sign in to vote.
4.93/5 (15 votes)
21 May 2010CC (Attr 3U)12 min read 100.8K   44   15
VB.NET and C# + twitto; a simple desktop Twitter

هذه المقالة متوفرة أيضا باللغة العربية، اقرأها هنا

Download twittoo; our sample application 

Contents

Contents of this writing:

  • Contents
  • Overview
  • Introduction
  • Twitter API
    • What Twitter API is
    • API Documentation
    • Things to be Kept into mind
  • API Methods
    • Method Address
    • Methods with Arguments
    • Methods Require Authentication
  • Twitter and .NET
    • Accessing the API
    • Authentication
    • Encoding  URIs
    • Business Objects
    • Retrieving Data
  • twittoo; Sample Application
    • Snapshots
    • Description
    • Interface
    • URL Shortening
    • Download

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.)
  • 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
<?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/" 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:

All of the four calls return the same results that should be like this:

XML
<?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
<?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.
  • 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:

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
<?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>

Notice that the sender and recipient are just user objects. It is worth mentioning that the direct_messages function return a list of direct_message objects (like the timeline functions that return list of status objects.)

Twitter and .NET

Now, you have a solid understanding of the Twitter API and how you can access it. Now, let’s utilize this API into our .NET applications.

Accessing the API

To access the API from your .NET application you need to create a HTTP request and send it to the server and wait for server response. Let’s consider an example. The following code snippet connects to the server and retrieves a System.Xml.XmlDocument that contains the returned data.

C#
// C# Code

public static Main()
{
    GetResponse("http://api.twitter.com/1/statuses/public_timeline.xml");
}

public static XmlDocument GetResponse(string uri)
{
    WebRequest req = WebRequest.Create(new Uri(uri));

    XmlDocument doc = new XmlDocument();
    doc.Load(req.GetResponse().GetResponseStream());
    return doc;
}
' VB.NET Code

Sub Main()
    GetResponse("http://api.twitter.com/1/statuses/public_timeline.xml")
End Sub

Public Function GetResponse(ByVal uri As String) As XmlDocument

    Dim req As WebRequest = WebRequest.Create(New Uri(uri))

    Dim doc As New XmlDocument()
    doc.Load(req.GetResponse().GetResponseStream())

    Return doc
End Function 

We have used the System.Xml.WebRequest class to create the request and to get the response (as an instance of System.Xml.WebResponse class) from the server. Once we get the XmlDocument, we can walk through the data and retrieve it.

Authentication

You can take one of two approaches to authenticate Twitter users:

  • OAuth Authentication:
    An authentication protocol that allows users to approve application to act on their behalf without sharing their password. As this function requires more overhead and requires your application to be registered in the Twitter clients’ directory, we would better use the second approach in our examples.
  • Basic Authentication:
    To provide authentication information in each request you make to the server. Unfortunately, Twitter announced that Basic Authentication will be removed later. 

Considering a method like statuses/update method that updates the status information of the user (i.e. sends a tweet) we would develop our previous code to be like this:

C#
// C# Code

public static void Main()
{
    GetResponse("http://api.twitter.com/1/statuses/update.xml?status=hello%20from%20the%20API",
        "elsheimy", "b@zzword", true);
}

public static XmlDocument GetResponse(string uri, string username, string password, bool post)
{
    WebRequest req = WebRequest.Create(new Uri(uri));
    if (post)
        req.Method = "POST";
    if ((username != null) && (username.Trim() != String.Empty) && (!String.IsNullOrEmpty(password)))
        req.Credentials = new NetworkCredential(username.Trim(), password);

    XmlDocument doc = new XmlDocument();
    doc.Load(req.GetResponse().GetResponseStream());
    return doc;
}
' VB.NET Code

Sub Main()
    GetResponse("http://api.twitter.com/1/statuses/update.xml?status=hello%20from%20the%20API", "elsheimy", "b@zzword", True)
End Sub

Public Function GetResponse(ByVal uri As String, ByVal username As String, ByVal password As String, ByVal post As Boolean) As XmlDocument
    Dim req As WebRequest = WebRequest.Create(New Uri(uri))
    If (post) Then
        req.Method = "POST"
    End If
    If ((username <> Nothing) And (username.Trim() <> String.Empty) And (Not String.IsNullOrEmpty(password))) Then
        req.Credentials = New NetworkCredential(username.Trim(), password)
    End If

    Dim doc As XmlDocument = New XmlDocument()
    doc.Load(req.GetResponse().GetResponseStream())

    Return doc
End Function 

Notice how we set the HTTP method based on the function requirements. It is worth mentioning that a status should not exceed 140 UTF-8-encoded characters.

Encoding URIs

Have you noticed the previous code? It tries to post the update “hello from the API”. Because the text is included with the URI, special handling to the text should be carried. This special handling for text included in URIs is usually called Percent-encoding or URL Encoding. This encoding replaces unsafe characters with their hexadecimal values preceded by percentage (%) signs. Unsafe characters are those somewhat conflicted with URI special characters. For example, if we would encode the text “hello from the API” we would end up with “hello%20from%20the%20API”.

There are many unsafe characters that should be percent-encoded including $, +, &, :, and =. A nice discussion of URL Encoding can be found in the article URL Encoding by Brian Wilson.

Once we get the idea, we can create our percent-encoding class that encodes/decodes strings:

C#
// C# Code

private static string[,] _chars = new string[,]
        {
        { "%", "%25" },     // this is the first one
        { "$" , "%24" },
        { "&", "%26" },
        { "+", "%2B" },
        { ",", "%2C" },
        { "/", "%2F" },
        { ":", "%3A" },
        { ";", "%3B" },
        { "=", "%3D" },
        { "?", "%3F" },
        { "@", "%40" },
        { " ", "%20" },
        { "\"" , "%22" },
        { "<", "%3C" },
        { ">", "%3E" },
        { "#", "%23" },
        { "{", "%7B" },
        { "}", "%7D" },
        { "|", "%7C" },
        { "\\", "%5C" },
        { "^", "%5E" },
        { "~", "%7E" },
        { "[", "%5B" },
        { "]", "%5D" },
        { "`", "%60" } };

public static string EncodeUrl(string url)
{
    for (int i = 0; i < _chars.GetUpperBound(0); i++)
        url = url.Replace(_chars[i, 0], _chars[i, 1]);

    return url;
}

public static string DecodeUrl(string url)
{
    for (int i = 0; i < _chars.GetUpperBound(0); i++)
        url = url.Replace(_chars[i, 1], _chars[i, 0]);

    return url;
}
' VB.NET Code
Public Module UrlEncoder

    Private _chars(,) As String = _
        { _
        {"%", "%25"}, _
        {"$", "%24"}, _
        {"&", "%26"}, _
        {"+", "%2B"}, _
        {",", "%2C"}, _
        {"/", "%2F"}, _
        {":", "%3A"}, _
        {";", "%3B"}, _
        {"=", "%3D"}, _
        {"?", "%3F"}, _
        {"@", "%40"}, _
        {" ", "%20"}, _
        {"\", "%22"}, _
        {"<", "%3C"}, _
        {">", "%3E"}, _
        {"#", "%23"}, _
        {"{", "%7B"}, _
        {"}", "%7D"}, _
        {"|", "%7C"}, _
        {"\", "%5C"}, _
        {"^", "%5E"}, _
        {"~", "%7E"}, _
        {"[", "%5B"}, _
        {"]", "%5D"}, _
        {"'", "%60"}}

    Public Function EncodeUrl(ByVal url As String) As String
        For i As Integer = 0 To _chars.GetUpperBound(0) - 1
            url = url.Replace(_chars(i, 0), _chars(i, 1))
        Next

        Return url
    End Function

    Public Function DecodeUrl(ByVal url As String) As String
        For i As Integer = 0 To _chars.GetUpperBound(0) - 1
            url = url.Replace(_chars(i, 1), _chars(i, 0))
        Next

        Return url
    End Function

End Module 

For clearness, we have included the encoded string of each character along with the character itself. You don’t have to do this. You can convert the character to a number and just output the number in hex format.

Now we could change the code that updates the status to the following:

C#
// C# Code

public static void Main()
{
    string uri;
    string text = UrlEncoder.EncodeUrl("hello from the API");
    uri = "http://api.twitter.com/1/statuses/update.xml?status=" + text;
    GetResponse(uri, "elsheimy", "b@zzwrd", true);
}
' VB.NET Code

Public Sub Main()

    Dim uri As String
    Dim text As String = UrlEncoder.EncodeUrl("hello from the API")
    uri = "http://api.twitter.com/1/statuses/update.xml?status=" & text
    GetResponse(uri, "elsheimy", "b@zzwrd", True)
End Sub 

.NET includes a nice function that escapes (encodes) a URI, System.Uri.EscapeUriString() function. However, this function does not encode all unsafe characters.

Business Objects

Once you know the structure of the XML data returned, you can create your business objects that would encapsulate this data. The following are the three classes that would represent our three core objects, the user, the status, and the message.

C#
// C# Code

public structure TwitterUser
{
    public long ID;
    public string Name;
    public string ScreenName;
    public string Location;
    public string Description;
    public string ProfileImage;
    public string Url;
    public bool IsProtected;
    public long FollowersCount;
    public long FriendsCount;
    public string CreatedAt;
    public long FavoritesCount;
    public bool Verified;
    public bool Following;
    public long StatusCount;
}

public structure TwitterStatus
{
    public string CreatedAt;
    public long ID;
    public string Text;
    public string Source;
    public bool Truncated;
    public long InReplyToStatusID;
    public long InReplyToUserID;
    public bool Favorited;
    public string InReplyToScreenName;
    public TwitterUser User;
}

public structure TwitterMessage
{
    public long ID;
    public long SenderID;
    public long SenderScreenName;
    public long RecipientID;
    public long RecipientScreenName;
    public string Text;
    public string CreatedAt;
    public TwitterUser Sender;
    public TwitterUser Recipient;
}
' VB.NET Code

Public Structure TwitterUser
    Public ID As Long
    Public Name As String
    Public ScreenName As String
    Public Location As String
    Public Description As String
    Public ProfileImage As String
    Public Url As String
    Public IsProtected As Boolean
    Public FollowersCount As Long
    Public FriendsCount As Long
    Public CreatedAt As String
    Public FavoritesCount As Long
    Public Verified As Boolean
    Public Following As Boolean
    Public StatusCount As Long
End Structure

Public Structure TwitterStatus
    Public CreatedAt As String
    Public ID As Long
    Public Text As String
    Public Source As String
    Public Truncated As Boolean
    Public InReplyToStatusID As Long
    Public InReplyToUserID As Long
    Public Favorited As Boolean
    Public InReplyToScreenName As String
    Public User As TwitterUser
End Structure

Public Structure TwitterMessage

    Public ID As Long
    Public SenderID As Long
    Public SenderScreenName As Long
    Public RecipientID As Long
    Public RecipientScreenName As Long
    Public Text As String
    Public CreatedAt As String
    Public Sender As TwitterUser
    Public Recipient As TwitterUser
End Structure 

Retrieving Data

Now you can walk through the XML data and get that data inside your objects. The following code returns a list of statuses from your friends’ timeline.

C#
// C# Code

public static void Main()
{
    GetStatuses("elsheimy", "b@zzword");
}

public static List GetStatuses(string username, string password)
{
    XmlNode node = GetResponse("http://api.twitter.com/1/statuses/friends_timeline.xml",
        username, password, true);

    List lst = new List(node.ChildNodes.Count);

    foreach (XmlNode nd in node.ChildNodes)   // for each status
        lst.Add(HandleStatus(nd));

    return lst;
}

public static TwitterStatus HandleStatus(XmlNode nd)
{
    // HandleNumber, FormatText, HandleBool
    // are just functions that converts strings
    // to numbers, decoded strings, and bool
    TwitterStatus status = new TwitterStatus(
        nd["created_at"].InnerText,
        HandleNumber(nd["id"]),
        FormatText(nd["text"]),
        FormatText(nd["source"]),
        HandleBool(nd["truncated"]),
        HandleNumber(nd["in_reply_to_status_id"]),
        HandleNumber(nd["in_reply_to_user_id"]),
        HandleBool(nd["favorited"]),
        FormatText(nd["in_reply_to_screen_name"]),
        HandleUser(nd["user"]));
    return status;
}

public static TwitterUser HandleUser(XmlNode nd)
{
    // HandleNumber, FormatText, HandleBool
    // are just functions that converts strings
    // to numbers, decoded strings, and bool
    long id = HandleNumber(nd["id"]);
    TwitterUser user;

    user = new TwitterUser(
        id,
        FormatText(nd["name"]),
        FormatText(nd["screen_name"]),
        FormatText(nd["location"]),
        FormatText(nd["description"]),
        nd["profile_image_url"].InnerText,
        nd["url"].InnerText,
        HandleBool(nd["protected"]),
        HandleNumber(nd["followers_count"]),
        HandleNumber(nd["friends_count"]),
        nd["created_at"].InnerText,
        HandleNumber(nd["favourites_count"]),
        HandleBool(nd["verified"]),
        HandleBool(nd["following"]),
        HandleNumber(nd["statuses_count"]));

    return user;
}
' VB.NET Code

Sub Main()
    GetStatuses("elsheimy", "b@zzword")
End Sub

Public Function GetStatuses(ByVal username As String, ByVal password As String) As List(Of TwitterStatus)
    Dim node As XmlNode = GetResponse("http://api.twitter.com/1/statuses/friends_timeline.xml", username, password, True)

    Dim lst As New List(Of TwitterStatus)(node.ChildNodes.Count)

    For Each nd As XmlNode In node.ChildNodes
        lst.Add(HandleStatus(nd))
    Next

    Return lst
End Function

Public Function HandleStatus(ByVal nd As XmlNode) As TwitterStatus
    ' HandleNumber, FormatText, HandleBool
    ' are just functions that converts strings
    ' to numbers, decoded strings, and bool

    Dim status As New TwitterStatus( _
            nd("created_at").InnerText, _
            HandleNumber(nd("id")), _
            FormatText(nd("text")), _
            FormatText(nd("source")), _
            HandleBool(nd("truncated")), _
            HandleNumber(nd("in_reply_to_status_id")), _
            HandleNumber(nd("in_reply_to_user_id")), _
            HandleBool(nd("favorited")), _
            FormatText(nd("in_reply_to_screen_name")), _
            HandleUser(nd("user")))

    Return status
End Function

Public Function HandleUser(ByVal nd As XmlNode) As TwitterUser
    ' HandleNumber, FormatText, HandleBool
    ' are just functions that converts strings
    ' to numbers, decoded strings, and bool
    Dim id As Long = HandleNumber(nd("id"))

    Dim user As New TwitterUser( _
        id, _
        FormatText(nd("name")), _
        FormatText(nd("screen_name")), _
        FormatText(nd("location")), _
        FormatText(nd("description")), _
        nd("profile_image_url").InnerText, _
        nd("url").InnerText, _
        HandleBool(nd("protected")), _
        HandleNumber(nd("followers_count")), _
        HandleNumber(nd("friends_count")), _
        nd("created_at").InnerText, _
        HandleNumber(nd("favourites_count")), _
        HandleBool(nd("verified")), _
        HandleBool(nd("following")), _
        HandleNumber(nd("statuses_count")))
    Return user
End Function 

twittoo, Sample Application

twitto, is our WinForms sample application that utilizes the Twitter API. This is just a very simple application with basic functionalities.

Snapshots

The following are snapshots of the application:

Figure 2 - twitto 0

Figure 3 - twitto 1

Figure 4 - twitto 2

Figure 5 - twitto 3

Figure 6 - twitto 4

Figure 7 - twitto 5

Description

This application was created using C# and WinForms 2.0 technology; it allows the user to navigate through his friends’ timeline, mentions, direct messages, retweets, and friends, and to update his status, reply to tweets, retweets, and to direct messages. Data is not refreshed automatically, the user have to click the “refresh” button. (You can create your own routine that creates a timer that updates the data automatically.)

Interface

As you see, the application uses just the Windows Common Controls all around the application; no 3rd party controls were used.

To represent a status, message, or a user, the application overuses the System.Windows.Forms.TableLayoutPanel control to represent each status, message, or user. It consists of four columns and two rows. The following figure shows how the control is laid-out.

Figure 8 - TableLayoutPanel Status Template

URL Shortening

twittoo, has a very nice feature, it allows the user to insert a shortened URL into his tweets. For this to work, the application makes use of http://is.gd URL shortening service. The following is the function that utilizes the http://is.gd API:

C#
// C# Code

public static string Shorten(string url)
{
    if (!System.Text.RegularExpressions.Regex.IsMatch
        (url, @"(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?"))
        throw new FormatException("The URL you specificed is not in the current format.");

    url = Uri.EscapeUriString(url);
    string reqUri = String.Format(@"http://is.gd/api.php?longurl={0}", url);

    WebRequest req = WebRequest.Create(reqUri);
    req.Timeout = 5000;
    using (System.IO.StreamReader reader =
        new System.IO.StreamReader(req.GetResponse().GetResponseStream()))
    {
        return reader.ReadLine();
    }
}
' VB.NET

Public Function Shorten(ByVal url As String) As String
    If (Not System.Text.RegularExpressions.Regex.IsMatch _
        (url, "(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&;:/~\+#]*[\w\-\@?^=%&;/~\+#])?")) Then
        Throw New FormatException("The URL you specificed is not in the current format.")
    End If

    url = Uri.EscapeUriString(url)
    Dim reqUri As String = String.Format("http://is.gd/api.php?longurl={0}", url)

    Dim req As WebRequest = WebRequest.Create(reqUri)
    req.Timeout = 5000

    Dim reader As New System.IO.StreamReader(req.GetResponse().GetResponseStream())
    url = reader.ReadLine()
    reader.Close()

    Return url
End Function 

Download 

Download twittoo; our sample application


Filed under: CodeProject, Twitter Tagged: .NET, API, CodeProject, CSharp, Products, Samples, Twitter, WinForms

License

This article, along with any associated source code and files, is licensed under The Creative Commons Attribution 3.0 Unported License


Written By
Technical Lead
Egypt Egypt
Mohammad Elsheimy is a developer, trainer, and technical writer currently hired by one of the leading fintech companies in Middle East, as a technical lead.

Mohammad is a MCP, MCTS, MCPD, MCSA, MCSE, and MCT expertized in Microsoft technologies, data management, analytics, Azure and DevOps solutions. He is also a Project Management Professional (PMP) and a Quranic Readings college (Al-Azhar) graduate specialized in Quranic readings, Islamic legislation, and the Arabic language.

Mohammad was born in Egypt. He loves his machine and his code more than anything else!

Currently, Mohammad runs two blogs: "Just Like [a] Magic" (http://JustLikeAMagic.com) and "مع الدوت نت" (http://WithdDotNet.net), both dedicated for programming and Microsoft technologies.

You can reach Mohammad at elsheimy[at]live[dot]com

Comments and Discussions

 
QuestionTwitter API Pin
squarenixx@msn.com21-Jun-13 18:23
squarenixx@msn.com21-Jun-13 18:23 
AnswerRe: Twitter API Pin
kiquenet.com20-Oct-14 11:24
professionalkiquenet.com20-Oct-14 11:24 
Question[My vote of 1] twittoo - cannot login Pin
Garry Lowther12-Nov-12 23:33
Garry Lowther12-Nov-12 23:33 
GeneralOAuth Pin
Daniel Lo Nigro26-Apr-10 14:12
Daniel Lo Nigro26-Apr-10 14:12 
GeneralRe: OAuth Pin
Mohammad Elsheimy27-Apr-10 2:06
Mohammad Elsheimy27-Apr-10 2:06 
GeneralMessage Closed Pin
19-Oct-10 4:39
e-sushi19-Oct-10 4:39 
GeneralRe: OAuth Pin
Mohammad Elsheimy19-Oct-10 6:31
Mohammad Elsheimy19-Oct-10 6:31 
GeneralRe: OAuth Pin
SoftexNL3-May-11 6:19
SoftexNL3-May-11 6:19 
GeneralRe: OAuth Pin
e-sushi18-Aug-14 1:52
e-sushi18-Aug-14 1:52 
GeneralSource Code Pin
bobfox22-Apr-10 13:36
professionalbobfox22-Apr-10 13:36 
GeneralRe: Source Code Pin
Mohammad Elsheimy23-Apr-10 1:07
Mohammad Elsheimy23-Apr-10 1:07 
GeneralRe: Source Code Pin
willempipi11-Oct-12 23:39
willempipi11-Oct-12 23:39 
GeneralNeat. Pin
Diamonddrake22-Apr-10 10:11
Diamonddrake22-Apr-10 10:11 
GeneralRe: Neat. Pin
Mohammad Elsheimy23-Apr-10 1:06
Mohammad Elsheimy23-Apr-10 1:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.