Home > Code Samples, Coding > Twitter API with C# example(part 1)

Twitter API with C# example(part 1)

sunflower

Currently I am involved in a project where number of things have to be done using Twitter API. What really amazes me today is the quality of API’s available to developers. Twitter is a perfect example of such API. I will try to publish some code that I used to accomplish several tasks starting with getting the details of a Twitter user providing you are aware of the user password and username.

Following code will retrieve a variety of information about a Twitter account:

  |  copy code |? 
01
02
public string FetchUserDetailsAsXml(string userName, string password, string IDorScreenName)
03
{
04
 
05
        if(string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password))
06
        {
07
            throw new ArgumentException("userName or Password are not supplied, that is not good.");
08
        }
09
 
10
    string url = string.Format(TwitterBaseUrlFormat, "users", "show" + "/" + IDorScreenName, "xml");
11
 
12
    using (var client = new WebClient())
13
    {       
14
        client.Credentials = new NetworkCredential(userName, password);
15
 
16
        try
17
        {
18
            using (var stream = client.OpenRead(url))
19
            {
20
                using (var reader = new StreamReader(stream))
21
                {
22
                    return reader.ReadToEnd();
23
                }
24
            }
25
        }
26
        catch (WebException weex)
27
        {
28
            if (weex.Response is HttpWebResponse)
29
            {
30
                if ((weex.Response as HttpWebResponse).StatusCode == HttpStatusCode.NotFound)
31
                {
32
                    return null;
33
                }
34
            }
35
            throw;
36
        }
37
    }
38
 
39
 }
40

That’s pretty much it. Need System.Net and System.Web for this, obviously. What you get out of that looks like this:

  |  copy code |? 
01
02
 
03
- <user>
04
  <id>14381487</id>
05
  <name>pzubkov</name>
06
  <screen_name>pzubkov</screen_name>
07
  <location>Ontario, Canada</location>
08
  <description>Coder, mostly .net</description>
09
<profile_image_url>http://s3.amazonaws.com/twitter_production/profile_images/56116140/Me_normal.jpg</profile_image_url>
10
  <url>http://www.paul-zubkov.com</url>
11
<protected>false</protected>
12
  <followers_count>39</followers_count>
13
<profile_background_color>709397</profile_background_color>
14
<profile_text_color>333333</profile_text_color>
15
<profile_link_color>FF3300</profile_link_color>
16
<profile_sidebar_fill_color>A0C5C7</profile_sidebar_fill_color>
17
<profile_sidebar_border_color>86A4A6</profile_sidebar_border_color>
18
  <friends_count>61</friends_count>
19
  <created_at>Mon Apr 14 05:08:22 +0000 2008</created_at>
20
  <favourites_count>0</favourites_count>
21
  <utc_offset>-18000</utc_offset>
22
  <time_zone>Eastern Time (US  Canada)</time_zone>
23
<profile_background_image_url>http://static.twitter.com/images/themes/theme6/bg.gif</profile_background_image_url>
24
<profile_background_tile>false</profile_background_tile>
25
  <statuses_count>49</statuses_count>
26
  <notifications>false</notifications>
27
  <verified>false</verified>
28
  <following>false</following>
29
- <status>
30
  <created_at>Wed Jul 01 19:15:11 +0000 2009</created_at>
31
  <id>2424620582</id>
32
  <text>twitter api rocks, this is sweet</text>
33
  <source></source>web
34
<truncated>false</truncated>
35
  <in_reply_to_status_id>
36
  <in_reply_to_user_id>
37
  <favorited>false</favorited>
38
  <in_reply_to_screen_name>
39
  </in_reply_to_screen_name>
40
  </in_reply_to_user_id>
41
</in_reply_to_status_id></status></user>

As we can see from above, I am not an avid Twitter user myself, but I find that the API produced by Twitter is extremely well done. In Part 2 and 3 I will cover sending new update and getting followers.

  1. No comments yet.
  1. No trackbacks yet.

Spam Protection by WP-SpamFree