Twitter API 1.1 oAuth adjustments for Streaming information

Recently saw myself in this predicament. Twitter changed their authentication mechanism from basic username and password to an oAuth 1.0a integrated mechanism.

This meant that one of the apps I am working on will need modification. From what used to be about 6 lines of code to I don’t know what.

So I head over to the twitter api documentation website and did a lot of reading.

The process now is to register your application to the Twitter developers page and get your oAuth keys from there. There is an oAuth Tool tab there where one can generate the needed oAuth credentials.

And then you see something… the request header needs a SIGNATURE, which is not part of what the tool gives. There is documentation on how to create a signature, a whole document that one would need to read to get to understand how to create your own password. It is my first time to code for something with oAuth and I was really confused.

Well anyways, I also needed to do a lot of digging on the .Net Framework on how to “Percent Encode” strings, generate timestamps etc. to create the basestring and signature. Ended up mashing code from stuff I found on codeplex, stackoverflow and some blogs.

So, here is the helper class that I will be using.

using System;

using System.Collections.Generic;

using System.IO;

using System.Linq;

using System.Text;


namespace StreamInsight.Demos.Twitter.Common.Helpers






StreamReader TwitterCall(string Method, string ResourceUrl, IEnumerable<KeyValuePair<string, string>> RequestParameters)


// oauth application keys

var oauth_token = “token goes here”;

var oauth_token_secret = “secret goes here”;

var oauth_consumer_key = “consumer key”;

var oauth_consumer_secret = “consumer secret”;

// oauth implementation details

var oauth_version = “1.0”;

var oauth_signature_method = “HMAC-SHA1”;


// unique request details

var oauth_nonce = Convert.ToBase64String(

new System.Text.ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString()));

var timeSpan = DateTime.UtcNow

DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);

var oauth_timestamp = Convert.ToInt64(timeSpan.TotalSeconds).ToString();


//setup the parameters that we will be putting in the authorization

var authorizationParameters = new
List<KeyValuePair<string, string>>() {

KeyValuePair<string, string>(“oauth_consumer_key”, oauth_consumer_key),

KeyValuePair<string, string>(“oauth_nonce”,oauth_nonce),

KeyValuePair<string, string>(“oauth_signature_method”,oauth_signature_method),

KeyValuePair<string, string>(“oauth_timestamp”,oauth_timestamp),

KeyValuePair<string, string>(“oauth_token”,oauth_token),

KeyValuePair<string, string>(“oauth_version”,oauth_version)



        var allParameters = authorizationParameters.Union(RequestParameters).OrderBy(tmp => tmp.Key);


var baseString = string.Join(“&”, allParameters.Select(p => string.Format(“{0}={1}”, p.Key, Uri.EscapeDataString(p.Value))));


baseString = string.Format(“{0}&{1}&{2}”, Method, Uri.EscapeDataString(ResourceUrl), Uri.EscapeDataString(baseString));


var compositeKey = string.Format(“{0}&{1}”, Uri.EscapeDataString(oauth_consumer_secret), Uri.EscapeDataString(oauth_token_secret));


//use our composite key to get the auth signature

string oauth_signature = null;

using (System.Security.Cryptography.HMACSHA1 hasher = new System.Security.Cryptography.HMACSHA1(System.Text.ASCIIEncoding.ASCII.GetBytes(compositeKey)))


oauth_signature = Convert.ToBase64String(




// create the request header

var headerFormat = “OAuth oauth_nonce=\”{0}\”, oauth_signature_method=\”{1}\”, “ +

“oauth_timestamp=\”{2}\”, oauth_consumer_key=\”{3}\”, “ +

“oauth_token=\”{4}\”, oauth_signature=\”{5}\”, “ +



//get the actual string version of the auth header and its values

var authHeader = string.Format(headerFormat,












System.Net.ServicePointManager.Expect100Continue = false;


//add our query string parameters to our url

var parameterString = string.Join(“&”, RequestParameters.Select(p => string.Format(“{0}={1}”, p.Key, Uri.EscapeDataString(p.Value))));

ResourceUrl += “?” + parameterString;



//create our request

System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(ResourceUrl);


//set our info

request.Headers.Add(“Authorization”, authHeader);

request.Method = Method;

request.ContentType = “application/x-www-form-urlencoded”;

request.PreAuthenticate = true;

request.AllowWriteStreamBuffering = true;

//get the response and return the result from the stream

var response = (System.Net.HttpWebResponse)request.GetResponse();

var reader = new System.IO.StreamReader(response.GetResponseStream());

return reader;




StreamReader TwitterCallGet(string ResourceUrl, IEnumerable<KeyValuePair<string, string>> RequestParameters)


return TwitterCall(“GET”, ResourceUrl, RequestParameters);



StreamReader TwitterCallPost(string ResourceUrl, IEnumerable<KeyValuePair<string, string>> RequestParameters)


return TwitterCall(“POST”, ResourceUrl, RequestParameters);





You will be calling it as:


StreamReader results = TwitterHelper.TwitterCallPost(“the url that you need“, new
KeyValuePair<string, string>[] { new
KeyValuePair<String, String>(“track”, “your parameters”) });



I hope it works! 😀





This entry was posted in Computers and Internet. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s