Thursday, January 15, 2009

How to update your status

Here's how to use the 'update' status method.

// Make your API calls with this.
private Twitter twitter;

// The responses from the Twitter REST API go here.
private XmlDocument xmlresponse;

// NEVER hard code your password. 
// I'm just trying to keep it simple for the example.
twitter = new Twitter("onil", "{my password}");

// Make a call to the 'update' method to post your status.
xmlresponse = twitter.StatusUpdate("Pooping.");

// Create a Status object using the XML response.
Status myStatus = new Status();
myStatus = (Status)XmlSerializerHelper.Deserialize(xmlresponse, typeof(Status));
            

// Convert the object back into XML.
// I find this useful for testing by printing the results to the screen.
xmlresponse = XmlSerializerHelper.Serialize(myStatus);


Here is a comparison of the response from the Twitter REST API against serializing the Status object:

Response from Twitter REST API

???<?xml version="1.0" encoding="UTF-8"?>
<status>
  <created_at>Fri Jan 16 04:17:39 +0000 2009</created_at>
  <id>1122908409</id>
  <text>Pooping.</text>
  <source>web</source>
  <truncated>false</truncated>
  <in_reply_to_status_id>
  </in_reply_to_status_id>
  <in_reply_to_user_id>
  </in_reply_to_user_id>
  <favorited>false</favorited>
  <in_reply_to_screen_name>
  </in_reply_to_screen_name>
  <user>
    <id>13871782</id>
    <name>Onil Patel</name>
    <screen_name>onil</screen_name>
    <location>Toronto, ON</location>
    <description>
    </description>
    <profile_image_url>http://s3.amazonaws.com/twitter_production/profile_images/50747572/me_normal.jpg</profile_image_url>
    <url>http://onil.homeip.net</url>
    <protected>false</protected>
    <followers_count>3</followers_count>
  </user>
</status>

Serialization of myStatus

???<?xml version="1.0" encoding="utf-8"?>
<status xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <created_at>Fri Jan 16 04:17:39 +0000 2009</created_at>
  <id>1122908409</id>
  <text>Pooping.</text>
  <source>web</source>
  <truncated>false</truncated>
  <user>
    <id>13871782</id>
    <name>Onil Patel</name>
    <screen_name>onil</screen_name>
    <location>Toronto, ON</location>
    <description />
    <profile_image_url>http://s3.amazonaws.com/twitter_production/profile_images/50747572/me_normal.jpg</profile_image_url>
    <url>http://onil.homeip.net</url>
    <protected>false</protected>
  </user>
</status>

As you can see, I'm missing some elements (highlighted in red) in my Status class. Twitter probably added these after I created it. They'll be added in a future release of TwitterXML.

2 comments:

  1. Hi, this works great thanks a lot.
    What method should I use to validate the login is good?

    ReplyDelete
  2. Sorry, I haven't coded anything for validating the login. You could try calling a method that requires authentication, and look at the response to see what's returned when authentication fails.

    ReplyDelete