September 20, 2010

Mittratter - Source Code


Ok, the Compilated App has been released here: Mittratter and its post related: http://mittrasw.blogspot.com/2010/09/mittratter-twitter-client-for-osx.html. What would help to other programmers trying to implement oAuth into their developments will be the source code used by Mittratter to sign into Twitter's Servers. Also I will give a brief explanation of how does this thing work.

As I explained here and here, now Twitter uses the oAuth Procedure, well the code behind is quite tricky and might confuse at first sight. Mittratter is an application developed in Objective-C, so the code is going to display here helps to implement those functions into iPhone/iPod Touch/iPad developments as well. Don't forget to do a quick review of the Twitter API where few libraries and implementations could be found. Well, let's get started.


As part of the main project, I used the oAuthConsumer Library, developed by Jon Crosby, which is the source code that complains with the oAuth specifications using Objective-C as the main language. This doesn't stick only with Twitter, it could be helpful to integrate with other service providers like Facebook, Google, Yahoo, etc.



After that I searched, compiled, tried, take my hair away, I found this example that was really helpful, so I will keep almost the same sequence applied to Mittratter:

1.- Once the app has finished lunching, it will perform a credential verification, because the user could be authenticated after and we just load his preferences. If not, they we would have to make a "Request Token", as the first step to communicate with Twitter:

[TMTwit getToken:nil withURL:nil andState:0];

This code invokes the function "getToken:withURL:andState:", where the first value means the Pin value (unavailable yet), the URL if we are going to communicate with different provider and State means we are looking for the "Request Token"

2.- If everything went well with the step #1, we receive a response from twitter with the "Request Token" and Mittratter redirects the user to his favorite Browser in order to authenticate and get the "Pin Value"

3.- When the user comes back with the "Pin Value", Mittratter makes another call, but now it has one more value and a change in State:

[TMTwit getToken:pinValue withURL:nil andState:1];

4.- Again, we wanted that the response from Twitter Server were successful, so that now we receive an Authorize Key and Secret, this pair values helps us to authenticate in behalf of the user each time we want to talk as an intermediate between Twitter and the User. So then we save that information using provided functions with the oAuthConsumer and we are able to interact with Twitter.

5.- Finally, we are able to post updates with the call:

[TMTwit performUpdateWithText:[tweetText string]];

were we are passing the information the user provided in the text field from the main window of Mittratter, easier impossible!!.

As we can read inside the code, TMainController is in charge of handling the communication between the main window and the Twitter engine (called TTwitter.m), so TTwitter could be ported to other implementations and just make the pertinent calls to those desired functions.

Well after lot of write and nothing tangible, here is the source code, and do not forget to code the consumer key and secret at the top of the TTwitter.m file.

Questions???


Here are most of the links:

Mittratter compiled
Mittratter source code

oAuth Example
xAuth Example
Objective-C Communication Reference

Twitter oAuth procedure
Twitter API to Update Status
Twitter Libraries

No comments:

Post a Comment