Calling the Twitter API in C# - More Wally - Wallace B. McClure

Giving people what they want, More Wally. This is the technical and personal blog site of
Wallace B. (Wally) McClure.

This Blog


Technical Sites


More Wally - Wallace B. McClure

This blog will have all kinds of posts about Wally McClure. In it, there will be tons of .NET and computer programming posts as well as Wally's views on life in general. As you might guess, this site and blog help you get More Wally in your life. What more could anyone want? iPhone, Android, Xamarin.iOS, Xamarin.Android, Mobile, HTML5, .NET, ADO.NET, ASP.NET, AJAX, jQuery, jQuery Mobile, ASP.NET AJAX, and Windows Azure............follow me on twitter at Wally

Calling the Twitter API in C#

I've been working on this application to send out tweets at a particular time.  One thing thing I want to do is be able to look at the logged in user's timeline.  I've been playing with the ASP.NET 4.0 AJAX Previews, so I thought I would marry the two.  As a result, I wrote the WCF code below in C#.  Note that the password is hidden, so the code won't work until you add that in.  Basically, I make a request to get the friends timeline.  This gives me the most recent 20 posts by default. I store the result in an XmlDocument.  I need to get that data out as a complex object of type UserStatusSvc as I am returning an array of that type.  I used Linq to Xml for that.

        public UserStatusSvc[] GetUserTimeLine(string username)
            string url = "";
            //string user = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(username + ":" + password));
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            request.Method = "POST";
            request.Credentials = new NetworkCredential(username, password);
            WebResponse response = request.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream());
            string responseString = reader.ReadToEnd();
            XmlDocument xmld = new XmlDocument();
            XDocument document = XDocument.Parse(responseString);

            var query = from e in document.Root.Descendants()
                        where e.Element("user") != null
                        select new UserStatusSvc
                            UserName = e.Element("user").Element("screen_name").Value,
                            ProfileImage = e.Element("user").Element("profile_image_url").Value,
                            Status = HttpUtility.HtmlDecode(e.Element("text").Value),
                            StatusDate = e.Value.ParseDateTime().ToString()

            var users = (from u in query
                         where u.Status != ""
                         select u).ToList();

            return (users.ToArray());

Here's the definition of my UserStatusSvc object.

    public class UserStatusSvc
        public string UserName { get; set; }
        public string ProfileImage { get; set; }
        public string Status { get; set; }
        public string StatusDate { get; set; }

    public static class StringExtensions
        public static DateTime ParseDateTime(this string date)
            string dayOfWeek = date.Substring(0, 3).Trim();
            string month = date.Substring(4, 3).Trim();
            string dayInMonth = date.Substring(8, 2).Trim();
            string time = date.Substring(11, 9).Trim();
            string offset = date.Substring(20, 5).Trim();
            string year = date.Substring(25, 5).Trim();
            string dateTime = string.Format("{0}-{1}-{2} {3}", dayInMonth, month, year, time);
            DateTime ret = DateTime.Parse(dateTime);
            return ret;

Thanks to Tim Heuer for some code he posted on his blog to get me started.  I have primarily stolen his code and reposted it here.  :-)

PS. I modified this code example to remove the hokey CheckForNull(...).  It was stupid and not necessary.  I didn't realize I had left it in until I got hassled for it.  The line 'where Element("user") != null' resolved my problem.



Dew Drop - March 18, 2009 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop - March 18, 2009 | Alvin Ashcraft's Morning Dew

March 18, 2009 9:02 AM

Community Blogs said:

I've been working on this application to send out tweets at a particular time. One thing thing I

March 19, 2009 7:02 AM

Scott Hanselman's Computer Zen said:

April 3, 2009 3:06 AM

ASPInsiders said:

I just was at Quiznos hanging out with @QuiznosNick . He's a former Technology Executive who bought a

April 3, 2009 3:39 AM

A Simple .NET Twitter API Wrapper Using LINQ « Sean’s Stuff said:

Pingback from  A Simple .NET Twitter API Wrapper Using LINQ « Sean’s Stuff

April 4, 2009 2:15 AM

The Weekly Source Code 40 - TweetSharp and Introducing Tweet Sandwich | CHARGED's Digital Lifestyle at Work or Play said:

Pingback from  The Weekly Source Code 40 - TweetSharp and Introducing Tweet Sandwich | CHARGED's Digital Lifestyle at Work or Play

April 7, 2009 4:51 AM

social media said:

2. Limit number of tweets per day, and charge for anything over. This will probably sound horrible to most Twitterers, but it could work out. Craig’ s List charges for certain types of ads. This has the dual effect of creating a revenue stream while preventing

August 13, 2009 9:20 AM

10 Twitter API Tutorials. « The Domage said:

Pingback from  10 Twitter API Tutorials. «  The Domage

September 3, 2009 12:59 AM

.NET faster Twitter API Calls | Q Sites said:

Pingback from  .NET faster Twitter API Calls | Q Sites

July 18, 2013 3:30 AM
2006 - Wallace B. McClure
Powered by Community Server (Non-Commercial Edition), by Telligent Systems