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

image

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

image

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

image

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

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 = “https://stream.twitter.com/1.1/statuses/filter.json”;

  // 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}” +
“&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}&track={6}”;
var baseString = string.Format(baseFormat,
oauth_consumer_key,
oauth_nonce,
oauth_signature_method,
oauth_timestamp,
oauth_token,
oauth_version,
Uri.EscapeDataString(_config.Parameters)
);
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(
hasher.ComputeHash(ASCIIEncoding.ASCII.GetBytes(baseString)));
}
           // 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}\”, ” +
“oauth_version=\”{6}\””;
var authHeader = string.Format(headerFormat,
Uri.EscapeDataString(oauth_nonce),
Uri.EscapeDataString(oauth_signature_method),
Uri.EscapeDataString(oauth_timestamp),
Uri.EscapeDataString(oauth_consumer_key),
Uri.EscapeDataString(oauth_token),
Uri.EscapeDataString(oauth_signature),
Uri.EscapeDataString(oauth_version)
);
// 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 twitterbigdata.codeplex.com 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.