Twitter API - Get a list of your friends in C# - More Wally - Wallace B. McClure
in

MoreWally.com

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

This Blog

Syndication

Technical Sites

Archives

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

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; }
    }

Comments

 

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 needed to call the Twitter API and get a list of friends. I thought I could do something similar to

March 20, 2009 7:39 AM
 

DotNetShoutout said:

Thank you for submitting this cool story - Trackback from DotNetShoutout

March 20, 2009 6:33 PM
2006 - Wallace B. McClure
Powered by Community Server (Non-Commercial Edition), by Telligent Systems