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:

public string FetchUserDetailsAsXml(string userName, string password, string IDorScreenName)
{

if(string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password))
{
throw new ArgumentException("userName or Password are not supplied, that is not good.");
}

string url = string.Format(TwitterBaseUrlFormat, "users", "show" + "/" + IDorScreenName, "xml");

using (var client = new WebClient())
{
client.Credentials = new NetworkCredential(userName, password);

try
{
using (var stream = client.OpenRead(url))
{
using (var reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
}
catch (WebException weex)
{
if (weex.Response is HttpWebResponse)
{
if ((weex.Response as HttpWebResponse).StatusCode == HttpStatusCode.NotFound)
{
return null;
}
}
throw;
}
}

}

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

-
14381487
pzubkov
pzubkov
Ontario, Canada
Coder, mostly .net

http://s3.amazonaws.com/twitter_production/profile_images/56116140/Me_normal.jpg http://www.paul-zubkov.com

false 39

709397 333333 FF3300 A0C5C7 86A4A6 61
Mon Apr 14 05:08:22 +0000 2008
0
-18000
Eastern Time (US Canada)

http://static.twitter.com/images/themes/theme6/bg.gif false 49
false
false
false
-
Wed Jul 01 19:15:11 +0000 2009
2424620582
twitter api rocks, this is sweet
web

false

false



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