Infosys Microsoft Alliance and Solutions blog

« MYOC - Telephony with Twilio to Vote | Main | Win 7 - Manage Default Printers »

MYOC - Update Twitter Status

In this blog we’ll see in details how sending a tweet from a particular twitter account programmatically works on Azure.

1. Take twitter ID and password as input from user, encrypt it and generate twitter authorization key from it. This key will be used to authorize a twitter user when calling the service to send a tweet.
string authorizationKey = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(
                              twitterUserId + ":" +

2. Create the status message to be sent on the twitter account
string messageToSend = "Hi";

// the message has to be enclosed in between opening <status> and closing </status> tags
// if the message length is greater than 140, the message will be trimmed to 140 characters
// (including white spaces)
string status = "<status>" + messageToSend + "</status>";

3. Use the below mentioned twitter service URI for request
// this is the URI of twitter service to update status of a twitter account
Uri serviceUri = new Uri(@"");

4. Set the Expect100Continue property as false before sending the request to twitter service. Read the post to know why is it required.
System.Net.ServicePointManager.Expect100Continue = false;

5. Now create the request and add parameters to the request. The request body is encoded using UTF 8 encoding. There are saveral other methods to encode which provide encryption too. Please check this link for more details.
WebHeaderCollection header = new WebHeaderCollection();

// add twitter authorization key to request header
header.Add("Authorization", "Basic " + twitterAuthorizationKey);

// create request and add header
HttpWebRequest request = WebRequest.Create(serviceUri) as HttpWebRequest;
request.Headers = header;

// encode the request body
byte[] requestBodyBytes = Encoding.UTF8.GetBytes(status);

//add request parameters
request.ContentLength = requestBodyBytes.Length;
request.ContentType = "application/xml";
request.Method = "POST";

//create request stream to send to the twitter service
Stream requestStream = request.GetRequestStream();
      requestStream.Write(requestBodyBytes, 0, requestBodyBytes.Length);
6. Post the request to the twitter service URI and get the response
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
      Stream stream = response.GetResponseStream();
using (StreamReader reader = new StreamReader(stream))
             string contents = reader.ReadToEnd();

Following the above approach, one can send a tweet on his/her twitter account, which his/her followers can see in their updates.

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

Please key in the two words you see in the box to validate your identity as an authentic user and reduce spam.

Subscribe to this blog's feed

Follow us on

Blogger Profiles

Infosys on Twitter