Monthly Archives: August 2013

Using OAuth to connect to Streaming Twitter API

Twitter just updated their API’s to v1.1 and with that update, now require Open Authentication or OAuth to access their streaming API.  OAuth has been required to access their REST API for some time now but it is a new requirement for their streaming services.  The first step is to get your authentication token from twitter.  Login on their developers page and choose my applications from the dropdown menu


Chose create new application and fill out the form.  If you do not have a website or callback url just add a place holder for now.  After creating you application scroll to the bottom of the page and click create my access token


At the top of this page there are tabs, choose the OAuth tool and you will see the details of your token there.


We will use this token in the code snippet below.  This code is from the twitter streaming class in the demo located at

var oauth_consumer_key = “Enter your consumer key here”;
var oauth_consumer_secret = “Enter your consumer secret key here”;
var oauth_token = “Enter your token here”;
var oauth_token_secret = “Enter your token secret key here”;
var oauth_version = “1.0”;
var oauth_signature_method = “HMAC-SHA1”;
// unique request details
var oauth_nonce = Convert.ToBase64String(
new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString()));
var timeSpan = DateTime.UtcNow
– new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
var oauth_timestamp = Convert.ToInt64(timeSpan.TotalSeconds).ToString();
var resource_url = “”;

  // create oauth signature(this could be different for the normal Twitter API as well as any other social API’s
var baseFormat = “oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}” +
var baseString = string.Format(baseFormat,
baseString = string.Concat(“POST&”, Uri.EscapeDataString(resource_url), “&”, Uri.EscapeDataString(baseString));
var compositeKey = string.Concat(Uri.EscapeDataString(oauth_consumer_secret),
“&”, Uri.EscapeDataString(oauth_token_secret));
string oauth_signature;
using (HMACSHA1 hasher = new HMACSHA1(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}\”, ” +
var authHeader = string.Format(headerFormat,
// make the request
ServicePointManager.Expect100Continue = false;
var postBody = “track=”+_config.Parameters; // “screen_name=” + Uri.EscapeDataString(screen_name);//
resource_url += “?” + postBody;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(resource_url);
request.Headers.Add(“Authorization”, authHeader);
request.Method = “POST”;
request.ContentType = “application/x-www-form-urlencoded”;
request.PreAuthenticate = true;
request.AllowWriteStreamBuffering = true;
WebResponse response = request.GetResponse();
dreturn new StreamReader(response.GetResponseStream());
If you add your token and consumer information to the above code and use this as the twitter streaming class in the project at you will be able to access twitters streaming API.  This can also be used as a base when using OAuth v1 with any other social sites/API’s.