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?
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
-
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).
-
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
-
Here is a great example to integrate Twitter oAuth in your Titanium project http://ziodave.tumblr.com/post/746024933/titanium-implementation-of-twitter-oauth
-
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); }
-
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); }
-
Thank you!
-
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?
-
Still waiting for the xAuth approval by twitter in order to give that a try. Any ideas about the oAuth crash?
-
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)
-
I"m also getting the Can't find variable: PinFinder error, any thoughts?
-
I"m also getting the Can't find variable: PinFinder error, any thoughts?
-
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.
-
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
-
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.
-
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