C# Twitter API Connections and XML

Twitter

Twitter API can be a lot of fun, some people use PHP but we can also use C# to create a program that connects to twitter and completes a certain task. In this example, we'll create a Twitter API client that will contact Twitter and check the amount of followers of anyone you want to look up.

A lot of activities in twitter require you to log in, this is why we first ask for login credentials and then create a network credentials class to establish authentication. We use WebClient class to open a connection to http://www.twitter.com/users/screenname.xml which will return information about a certain user called "screenname".

Using stream, XmlTextReader, and StreamReader to interpret the response. Alternatively to XmlTextReader you may use XPath functions (which may be easier or harder depending on your task).

I decided to make this tutorial simple and not use any GUI to confuse you. This is the code:

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Net;
using System.IO;
using System.Xml;


namespace TAPI
{
    class TAPI
    {
        public void extractTwitterDetails(string user, string pass, string TName)  {
            if (string.IsNullOrEmpty(user) || string.IsNullOrEmpty(pass)){
                throw new ArgumentException("Username and password fields are empty");
            }
            string target = "http://twitter.com/users/"+TName+".xml";

            WebClient client = new WebClient();
            client.Credentials = new NetworkCredential(user, pass);

            try {
               Stream stream = client.OpenRead(target);
               StreamReader reader = new StreamReader(stream);
               XmlTextReader xml_read = new XmlTextReader(stream);
               while (xml_read.Read())
               {
                   xml_read.MoveToElement();
                   if (xml_read.Name == "followers_count")
                   {
                       Console.WriteLine("Followers: " + xml_read.ReadInnerXml().ToString());
                   }
               }
            } catch (WebException WebEx) {
                Console.WriteLine("Error: " + WebEx.Message);
                throw;
            }
           
        }

       
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome to Twitter API");
            Console.WriteLine("Username: ");
            string User = Console.ReadLine();
            Console.WriteLine("Password: ");
            string Pass = Console.ReadLine();
            Console.WriteLine("Who's followers do you want to see: ");
            string TwitterName = Console.ReadLine();

            TAPI t = new TAPI();
            t.extractTwitterDetails(User, Pass, TwitterName);
            return;
        }
    }
}

You can grab all sorts of information like this. Remember, to check out the Twitter API documentation for various xml responses from server. You can look at your own list of followers (http://www.twitter.com/statuses/followers.xml) or the list of people you follow (http://www.twitter.com/statuses/friends.xml), you can even follow other people by making a different function.

Dodd's picture

Cool, I didn't know it was

Cool, I didn't know it was worth doing it in C# or C++. I use PHP too.

Dan Killam's picture

Nice article Baran, I'm sure

Nice article Baran, I'm sure this will help many people figure out how to speed up their Twitter automation.

Matthew Malde's picture

Thank you for this tutorial

Thank you for this tutorial

Jake's picture

Hey, that's pretty cool. I

Hey, that's pretty cool. I never used Twitter API before, I'll give it a shot in C#

Bob's picture

Excellent! I think soon

Excellent! I think soon though, we'll need to use OAuth to connect to twitter instead of basic auth.

Baran Ornarli's picture

Nah, you won't for a while,

Nah, you won't for a while, but yes eventually they may switch permanently to that.

Why because there are several advantages to using OAuth:

  1. Easier to track bots (although its always been easy for them)
  2. People who use suspicious clients/websites won't get their account hacked (because you give the server your password) [I think TweetViewer was one of those stealing apps]
  3. More features, standardization of clients, better tracking

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account associated with the e-mail address you provide, it will be used to display your avatar.