Using OAuth to Send Updates to Twitter Using C#

12/9/2009

Well I'm not sure if it's better. To be honest it's a bit more difficult to set up initially but it's a bit more secure than the way I showed before. Anyway, this year (in fact early this year), Twitter started a transition to using OAuth. Since I'm new to messing with the service, I had no idea. But then again I'm pretty oblivious to most things, so I might have missed that information even if I had been an early adopter. That being said, I've talked about OAuth before and I'll assume that you've read the post here. If not take a look before looking at the code below. Anyway, enough stalling, let's jump into some code:

The code above uses the OAuth helper class (modified, from the post that I link to so go to the svn store on codeplex to see the latest version) along with a new REST helper class that I created to work with Twitter. But before I show the REST class, I should explain the code above. There are a number of functions here that you need to call prior to ever being able to send anything to Twitter. The first is GetRequestToken. This does what it sounds like. It gets a request token from the Twitter service. The request token is then used to register an app with a user. Prior to authorizing the app, you can't do a damn thing when using OAuth. The request token/token secret that is returned by the Twitter service are then used in the GetAuthorizationSite function. This function simply gets you the location that the user must go in order to authorize your app to work with their account. The user goes to that location, clicks on Allow and is then given a PIN number. Note that during all of this you have to hold onto the request token/secret token so that you can use them along with the PIN in the next function: GetAccessToken. This function actually trades in the request token that you're using for an access token/token secret. These are then used in all of the functions that actually do anything...

That's right, you just went through three steps just so you can start to use the service. It's annoying but on the bright side, it's much more secure. Not to mention it tends to make those who are paranoid trust you a bit more as they are no longer giving you their user name and password. Anyway, the other two functions in the class do two things. The first is a status update function. It's basically what I showed you before in the previous post but cleaned up and using OAuth instead of basic authentication. The second function allows you to query Twitter for a timeline of a single user. It returns an XML file that you'll need to parse, but it does get the job done for now. They should be fairly simple to follow... Well, they would be if I had shown you the REST class before now. So you're not completely confused here it is:

If you've ever dealt with a REST API before, it's pretty basic. But more or less it's divided into four functions, GET, PUT, POST, DELETE. It also allows for basic authentication if you need it. But basically you just load up the URL, any data that needs to be appended, user name and password and call the appropriate function. It in turn returns anything that the server sends back. So there you go, basic REST helper and Twitter helper classes. It's not that difficult to build off of them, so take a look, leave feedback, and happy coding.

Comments

JeroOctober 01, 2010 3:31 AMHi!thanks!! now I understand the way it works. But now I have a problem. When my program tries to get the request token, an exception occurs: "Error in remote server: 401 not authorized".The error trace is :Temp.GetRequestToken(out Token, out TokenSecret); --> string Value = RestHelper.GET(); --> SendRequest(HttpWebRequest Request) --> HttpWebResponse Response = Request.GetResponse() as HttpWebResponseWhen attempting to get the HttpWebResponse, the exception is launched. I've registered my app, the key and secretKey are OK. I don't know what's happening.Another cuestion: This process needs http, or https? Thanks a lot!James CraigSeptember 30, 2010 12:01 PMWell before you get to any code, you have to register your app with Twitter at: http://dev.twitter.com.Once you've done that, go into the information on their site for your app and pull off the consumer key and consumer key secret. When you have that you have to register your app with your account, this is done in a couple of steps.1) Get the Request TokenThis is done, with the following code:Twitter Temp = new Twitter();Temp.ConsumerKey="YOUR CONSUMER KEY";Temp.ConsumerKeySecret="YOUR CONSUMER SECRET";string Token="";string TokenSecret="";Temp.GetRequestToken(out Token, out TokenSecret);Temp.Token = Token;Temp.TokenSecret = TokenSecret;2) Store the Token/TokenSecret for later. You have to make the next couple of requests with it (store it in the session or user profile or something for web requests).3) Redirect (or JeroSeptember 30, 2010 6:46 AMHi, nice post, I'm searching for a long time for an example like this. But I can't make a simple Publish action to twitter, I'm not sure the methods that I've to call to make publishing.Please, can you post some example of execution code? Thank you.