Wednesday 15 May 2013

Twitter API 1.1 oAuth - Cut the crap and get it working :D

After lots of research, piecing together of all sorts of code, I finally managed to get a working version of oAuth API 1.1 in twitter. It still need a lot of refactoring, but you will get the idea. Its not pretty, but it works :D When I get time I might improve this and post a cleaner version ;) It turns out its is quite fussy about how you send the request with the request headers. This also uses JSON.net to parse the and dynamic type is quite interesting and is something I havent used before. This will allow you to databind to control like any other datasource which is useful and to return just the latest tweet you can literally just change the count to 1. Anyway enough waffle, you can download the source from: Download Sourcecode Or you can see the class below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TwitterTest;

namespace TwitterControl
{
public partial class TwitterControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
// Setup Request
Twitter.ScreenName = "Test"; // This can be any twitter account
Twitter.Count = 20;

// These can be found in your developers seciton of twitter dev.twitter.com and in your applications
Twitter.oAuthConsumerKey = "";
Twitter.oAuthConsumerSecret = "";
Twitter.oAuth_token = "";

// Generate Random Base 64 Code
Twitter.oAuth_nonce = Twitter.generateNonce();

// Signature Method
Twitter.oAuth_signature_method = "HMAC-SHA1";

// Timestamp
Twitter.oAuth_timestamp = DateTime.Now.ToString();

// API Version
Twitter.oAuth_version = "1.1";

twitterRepeater.DataSource = Twitter.RetrieveTweets();
twitterRepeater.DataBind();
}
}
}

There is an issue with adding count, it has to be added to base string, in due course I will update this, its just finding the time :\

2 comments:

Tim Acheson said...

That's nice, can I add this example to my open source working demo?

My demo includes a very simple working example of Twitter API 1.1 in ASP.NET.

Netferret said...

Yer sure go for it, I have another cleaner version I am working on, but I just haven't had time to make it as good as I would like.

Glad you liked it anyway, its a starting point if nothing else.