Follow me on Twitter at Twitter.com/wbm
FYI, I'm blogging most of my stuff over at More Wally now.
You might want to add my rss feed to your reader at:http://morewally.com/cs/blogs/wallym/rss.aspx
Twitter API - Get a list of your friends in C# - Wallace B. McClure

Wallace B. McClure

All About Wally McClure - The musings of Wallym on Web, HTML5, Mobile, Xamarin.iOS, Xamarin.Android, and Windows Azure.

News

Visual Studio Magazine Column Personal Blog

.NET

Book Authors

Business

Family

Friends

Georgia Tech Bloggers

Personal

Archives

Twitter API - Get a list of your friends in C#

I needed to call the Twitter API and get a list of friends.  I thought I could do something similar to getting a list of posts, but alas, I had some stupid error in my linq to xml code.  I futzed around with it for days to no avail.  Finally, I decided it was just easier to iterate through the XML using an XmlNode and fill my objects that way.  Hey, it just worked.  Like my previous example, this code uses C# and is in a WCF Service.  Note: As displayed, the code doesn't work, you will have to supply a password.

        [OperationContract]
        public Friends[] GetFriends(string username)
        {
            string url = "http://twitter.com/statuses/friends.xml";
            string user = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(username + ":" + password));
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            Friends frd;
            List<Friends> lf = new List<Friends>();

            request.Method = "POST";
            request.Credentials = new NetworkCredential(username, password);
            WebResponse response = request.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream());
            string responseString = reader.ReadToEnd();
            reader.Close();
            XmlDocument xmld = new XmlDocument();
            xmld.LoadXml(responseString);
            XDocument document = XDocument.Parse(responseString);

            foreach(XmlNode xmln in xmld.SelectNodes("users/user"))
            {
                frd = new Friends();
                frd.id = Convert.ToInt32(xmln["id"].InnerText);
                frd.name = xmln["name"].InnerText;
                frd.screen_name = xmln["screen_name"].InnerText;
                lf.Add(frd);
            }
            return (lf.ToArray());
        }
 

My custom object which I am streaming back looks like this:

    [DataContract]
    public class Friends
    {
        [DataMember]
        public string name { get; set; }
        [DataMember]
        public string screen_name { get; set; }
        [DataMember]
        public int id { get; set; }
    }

 Original Url: http://morewally.com/cs/blogs/wallym/archive/2009/03/18/twitter-api-get-a-list-of-your-friends-in-c.aspx

Comments

cibrax said:

Hey Wally,

Have you taken a look at this project, code.google.com/.../twitterbots ?

It provides a rich object model layered on top of the twitter API, so you do not have to worry about many details.

Pablo.

# March 20, 2009 9:01 AM

Wallym said:

There is a post coming up that explains why I'm writing to the api.

# March 22, 2009 3:58 PM