Appcelerator Blog

The Leading Resource for All Things Mobile

Twitter oAuth Implementation for Titanium Mobile

0 Flares 0 Flares ×

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('http://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('http://twitter.com/oauth/authorize?oauth_token=' +
        oAuthAdapter.getRequestToken('http://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.

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Email -- 0 Flares ×

13 Comments

  1. Will this work out of Titanium? I’m thinking of implementing something like it on my website.

  2. Kevin Whinnery

    This is engineered specifically for Titanium – for your website, a quick Google search reveals many good examples of doing this for webapps, including the following from the Twitter docs:

    http://apiwiki.twitter.com/OAuth-Examples

  3. @Kevin
    Thank you so much! 😀

  4. James

    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?

  5. Tim

    I got the same thing James. As soon as you hit the button, the app crashes.

  6. James

    I actually tracked down the problem to a bug with iOS and trying to recognise phone numbers in the page.

    I’ve created a ticket: https://appcelerator.lighthouseapp.com/projects/32238/tickets/1233-webview-crashes-on-twitter-oauth-pin-page-132

    Wonder if Apple will fix the bug, or you guys can add the workaround before twitter shuts off the HTTP Auth for it’s API in August.

  7. I don’t have any problems when I run the application on the iPhone. Seems to be a simulator issue.

  8. jrn

    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?

  9. jrn

    or do I have to make it a community project? :)

  10. Hi James,

    There is a bug in SDK 4.0. I published an update for the OAuth Adapter lib which solves this by setting the autoDetect property of the webview to AUTODETECT_NONE.

    You can get it here: http://code.google.com/p/oauth-adapter/wiki/UsageManualR4

    webView = Ti.UI.createWebView({
    url: pUrl,
    autoDetect:[Ti.UI.AUTODETECT_NONE]
    });

  11. After getting integrate with my app it returns the seven digit pin so pls. help me where to put this PIN in my app to get authorized and get access to it.

Comments are closed.

Sign up for updates!

Become a mobile leader. Take the first step to scale mobile innovation throughout your enterprise.
Get in touch
computer and tablet showing Appcelerator software
Start free, grow from there.
0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Email -- 0 Flares ×