I was hoping for a small code snippet that would let me do that without needing any third-party libraries, but the feedback from the lazy web suggested that jumping through the hoops of an OAuth implementation myself wasn't worth the effort.
The wisdom of crowds suggested Twitter4J as a simple alternative - and as the following code snippet shows - the most simple case is pleasantly simple to implement.
Twitter twitter = new TwitterFactory().getInstance();
AccessToken a = new AccessToken(oauth_token, oauth_token_secret);
twitter.setOAuthConsumer(consumer_token, consumer_secret);
twitter.setOAuthAccessToken(a);
twitter.updateStatus("If you're reading this on Twitter, it worked!");
In this instance I'm the only one who'll be using the app, so I'm dropping an auth token and auth token secret unique to my own Twitter login rather than going through the process required to obtain a user-specific auth token. If that matches your use-case you can grab those values by clicking "My Access Token" on the Twitter developer site after you've registered your app.
You can download Twitter4J for Android here. Then just add twitter4j-core-android-2.2.1.jar into your project as an external JAR.
Yay, good work!
ReplyDeleteusing Twitter4j with user login requires callbacks, for that i had to use single instance/single task, that caused activity stacks to go crazy i'm still trying to find a way around this. Do you have any suggestions for that?
ReplyDelete@demonreeper I took an android where one of the projects was to build a twitter client. The teacher posted the code here: https://github.com/thillerson/oreilly_android
ReplyDeleteThat should answer your callback question.
Excellent!!
ReplyDeleteI found the oauth hoops bearable with the Twitter example at:
ReplyDeletehttps://github.com/ddewaele/AndroidTwitterSample
OAuth does not need a callback - you can have the user login at Twitter to receive a pin, which is then uses to get the final auth token from twitter. If Twitter allows an app to use xAuth, even this pin thingy is not needed. My client Zwitscher (https://github.com/pilhuhn/ZwitscherA) does this for example
ReplyDelete@Heiko W. Rupp
ReplyDeleteWell pin method is kinda user unfriendly and i found a way to correct the activity problems :)
Looks pretty simple. Thanks for sharing!
ReplyDeleteI've been working on the same thing myself. I chose to use the scribe oauth library instead because it supports other networks besides twitter. Here is a sample showing a sign in with twitter button http://schwiz.net/blog/2011/using-scribe-with-android/
ReplyDelete@demonreeper use a WebViewClient to catch the callback, doesn't require an extra activity. See my sample in my comment above.
ReplyDelete@demonreeper : Can you elaborate on "I found a way to correct" please?
ReplyDeleteWe've done something similar to what @schwiz comments but using signpost http://code.google.com/p/oauth-signpost/
ReplyDeleteThe code is not available (yet, I hope we can open source the whole app soon enough), but some info could be found here: http://stackoverflow.com/questions/4783018/pin-in-twitter4j
I like your article very much.welcome to visit true religion uk!It may give you a surprise!
ReplyDeleteTill 29 April Twitter4j work on my project for log-in and getting the tweet data,but now it is not working,any thing changes on the Twitter4j API please reply..
ReplyDeleteI had a huge problem posting tweets, until I read what @Demonreeper said! This fixed my problem so wrote it up into a Tutorial if anyone else needs to Tweet from another users acc: http://goo.gl/99vpL
ReplyDeleteGood Work done thanks a lot man ....
ReplyDeletethat is such a nice tutorial.