Twitter API with C# example(part 1)

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:
-
-
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.