Twitter oAuth Implementation for Titanium Mobile

Colors In Bloom series. Artistic background made of fractal color textures for use with projects on imagination, creativity and design

We have a great community project to highlight today – David Riccitelli (follow @ziodave on Twitter here) has developed a pure JavaScript oAuth helper library for Titanium Mobile (today it’s iPhone only, but if you check out source on Google Code I’m sure one of us can rectify that).

If you’d like to use it, your first step is to register your application at Twitter’s developer site. Once you do that, you should be able to grab your application’s consumer key and shared secret.

Once your app is set up, you’re ready to integrate David’s Twitter library. Create a new Titanium Mobile project, and under the Resources directory, create a /lib folder. In this directory, you will place the required JavaScript files for David’s library – all three files are linked from David’s guide.

Your next step is to integrate Twitter for fun and profit! In app.js, drop in the following code to see a simple example in action:

var oAuthAdapter = new OAuthAdapter(
        '',
        '',
        'HMAC-SHA1');


// load the access token for the service (if previously saved)
oAuthAdapter.loadAccessToken('twitter');

oAuthAdapter.send('https://api.twitter.com/1/statuses/update.json', [
  ['status', 'hey @ziodave, I successfully tested the #oauth adapter with #twitter and @appcelerator #titanium!']
], 'Twitter', 'Published.', 'Not published.');

// if the client is not authorized, ask for authorization. 
// the previous tweet will be sent automatically after authorization
if (oAuthAdapter.isAuthorized() == false) {
    // this function will be called as soon as the application is authorized
    var receivePin = function() {
        // get the access token with the provided pin/oauth_verifier
        oAuthAdapter.getAccessToken('https://twitter.com/oauth/access_token');
        // save the access token
        oAuthAdapter.saveAccessToken('twitter');
};

    // show the authorization UI and call back the receive PIN function
    oAuthAdapter.showAuthorizeUI('https://twitter.com/oauth/authorize?oauth_token=' +
        oAuthAdapter.getRequestToken('https://twitter.com/oauth/request_token', [['oauth_callback', 'oob']]),
        receivePin, PinFinder.twitter);
}

You’ll find that once you have signed in once to Twitter, you will be able to successfully update your stream without re-authorizing, as David is storing your authorization token locally. Very cool stuff, thanks David! David also recently published this quick video demonstration of his Twitter library in action in his own (also cool) Titanium app, which is embedded below. Please show David some love for his very useful contribution!

Twitter OAuth in Titanium from David Riccitelli on Vimeo.

13 COMMENTS

  1. […] Twitter oAuth Implementation for Titanium Mobile « Appcelerator Developer Center We have a great community project to highlight today – David Riccitelli (follow @ziodave on Twitter here) has developed a pure JavaScript oAuth helper library for Titanium Mobile (today it’s iPhone only, but if you check out source on Google Code I’m sure one of us can rectify that). (tags: appcelerator code twitter) […]

  2. I’ve tried this on more than one machine, with both 1.3 and 1.3.2 (iPad App), and the Webview that presents the ALLOW / DENEY buttons crashes as soon as it gets to the Pin number page.

    Anyone else?

  3. Ehmm, I’d like to see the xAuth implementation for this.

    With xAuth I mean the browserless token exchange option where you just submit user and password once, get a token in return and store that one in your app. This is way more user friendly.

    I’ve implemented this in an app thru the MIT licensed XAuthTwitterEngine (http://github.com/aral/XAuthTwitterEngine)

    Any chance this gets ported into Titanium?

Comments are closed.