Wednesday, April 27, 2011

Using Twitter4J to Tweet in Android

So I'm working on a little project for Google I/O that requires, amongst other things, the ability to post status updates to Twitter from within an Android app. I asked about it on Twitter and a couple of people asked me to post the results (and associated code snippets) so here you go.

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.

16 comments:

  1. using 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
  2. @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

    That should answer your callback question.

    ReplyDelete
  3. I found the oauth hoops bearable with the Twitter example at:
    https://github.com/ddewaele/AndroidTwitterSample

    ReplyDelete
  4. 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
  5. @Heiko W. Rupp
    Well pin method is kinda user unfriendly and i found a way to correct the activity problems :)

    ReplyDelete
  6. Looks pretty simple. Thanks for sharing!

    ReplyDelete
  7. I'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
  8. @demonreeper use a WebViewClient to catch the callback, doesn't require an extra activity. See my sample in my comment above.

    ReplyDelete
  9. @demonreeper : Can you elaborate on "I found a way to correct" please?

    ReplyDelete
  10. We've done something similar to what @schwiz comments but using signpost http://code.google.com/p/oauth-signpost/

    The 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

    ReplyDelete
  11. I like your article very much.welcome to visit true religion uk!It may give you a surprise!

    ReplyDelete
  12. Anonymous12:26 pm BST

    Till 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..

    ReplyDelete
  13. I 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

    ReplyDelete
  14. Good Work done thanks a lot man ....
    that is such a nice tutorial.

    ReplyDelete