Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Twitter Status Update

I have tried to use the following code:

var url = "https://username:password@twitter.com/statuses/update.json";
var xhr = Titanium.Network.createHTTPClient();
xhr.onload = function() {
  // Handle response
};
xhr.open("POST",url);
xhr.send({ status:'My awesome tweet!' });

suggested in http://developer.appcelerator.com/question/11451/post-a-tweet. After running the app I get the following error:

>Error Domain=ASIHTTPRequestErrorDomain Code=3 "Authentication needed" UserInfo=0x7d881a0 {NSLocalizedDescription=Authentication needed}

What am I doing wrong?

— asked June 30th 2010 by Vassilis Papakonstantinou
  • authentication
  • error
  • twitter
0 Comments

16 Answers

  • I used the Xauth method mentioned below, very easy to implement. No web UI involved, looks more native :)

    http://developer.appcelerator.com/question/4581/custom-module

    — answered July 2nd 2010 by Daniel Lim
    permalink
    0 Comments
  • Even if you get this to work, twitter is phasing out basic auth in a few weeks. You will need to support OAuth in your app (which will take a bit more code and logic).

    — answered June 30th 2010 by Martijn Pannevis
    permalink
    0 Comments
  • I just found out about this "basic auth" phasing out. I've read about xAuth which is presumably easier than OAuth but still have to find some examples to work on. Any ideas/suggestions to help me build on will be much appreciated. Thanks

    — answered June 30th 2010 by Vassilis Papakonstantinou
    permalink
    0 Comments
  • Basic Authentication in Twitter will be disabled by mid-August 2010. Use OAuth as explained in these 10 lines of code with the OAuth Adapter:

    
    var oAuthAdapter = new OAuthAdapter(
            '<your-consumer-secret>',
            '<your-consumer-key>',
            '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);
    }
    
    — answered July 2nd 2010 by David Riccitelli
    permalink
    0 Comments
  • 10 lines of code with the OAuth Adapter:

    
    var oAuthAdapter = new OAuthAdapter(
            '<your-consumer-secret>',
            '<your-consumer-key>',
            '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);
    }
    
    — answered July 2nd 2010 by David Riccitelli
    permalink
    1 Comment
    • I successfully received PIN but where do I need to put this pin in code

      — commented August 13th 2012 by Umaid Saleem
  • Thank you!

    — answered July 2nd 2010 by Vassilis Papakonstantinou
    permalink
    0 Comments
  • I have used the code and when I run the app (on simulator) I get the authorization window but after I enter the credentials and press the "yes" button, the app crashes and exits the simulator. Any ideas?

    — answered July 2nd 2010 by Vassilis Papakonstantinou
    permalink
    0 Comments
  • Still waiting for the xAuth approval by twitter in order to give that a try. Any ideas about the oAuth crash?

    — answered July 3rd 2010 by Vassilis Papakonstantinou
    permalink
    0 Comments
  • David, I've set up your example, but I'm getting this:

    [WARN] Exception in event callback. {
    expressionBeginOffset = 2749;
    expressionCaretOffset = 2758;
    expressionEndOffset = 2758;
    line = 121;
    message = "Can't find variable: PinFinder";
    name = ReferenceError;
    sourceId = 250890664;
    sourceURL = "file://localhost/Users/HD/Library/Application%20Support/iPhone%20Simulator/4.0/Applications/9CEE1941-7D1E-48EC-8DD9-778B12D1C74E/twit.app/result.js";
    }
    

    It doesn't work in the simulator or the device.

    any ideas? (I'm on iOS4 and titan 1.4.0)

    — answered August 8th 2010 by Mark Smillie
    permalink
    0 Comments
  • I"m also getting the Can't find variable: PinFinder error, any thoughts?

    — answered August 8th 2010 by Paul Pounder
    permalink
    0 Comments
  • I"m also getting the Can't find variable: PinFinder error, any thoughts?

    — answered August 8th 2010 by Paul Pounder
    permalink
    0 Comments
  • I've got the xAuth version working (the oauth version described in detail here always crashes my app when I close the window, no clue why) but I have a bit of a newbie question: Now that I have the credentials I need how do I make a timeline request? Specifically could someone give me a hint on how to format an oauth request for the following: http://api.twitter.com/version/statuses/friends_timeline.format

    Once I get this last bit running I plan to strip down the app so others can use my code as an example, or even hopefully just plug right it right into their own projects.

    — answered August 9th 2010 by Russell Morgan
    permalink
    0 Comments
  • Russell, did you have any luck?

    I've managed to get authenticated, but can't use the credentials for getting timelines etc. Have you had any success?

    Regards

    Paul

    — answered August 17th 2010 by Paul Pounder
    permalink
    0 Comments
  • BirdHouse: Twitter OAuth for Android & iPhone

    To anyone would like to use Twitter or is having trouble using a Twitter script,

    Cross-Platform Support & Up-to-Date: BirdHouse has been tested on Android with Linux and Mac as well as on iPhone, both using the latest Titanium Mobile SDK (1.6.2). It has been tested by others and is being maintained to fix any issues.

    Go here for the code and go here for the test app.

    About: The script can send tweets, retrieve tweets, authenticate, "deauthenticate", and send custom API calls to Twitter. For sending tweets, a custom UI popup appears. It is designed to work similar to Titanium's Facebook module.

    David Riccitelli's oauth-adapter doesn't work on Android, but some have forked it to work, such as Ketan Majmudar (go here for his code), I have tried other OAuth scripts as well, but I have been more successful with BirdHouse.

    — answered April 19th 2011 by Joe iEntry
    permalink
    0 Comments
  • For those that have problems with "Can't find variable:PinFinder" here's the solution

    http://developer.appcelerator.com/question/52451/oauth-by-david-riccitelli

    — answered March 28th 2012 by Ionut Pisla
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.