Twitter OAuth on Java AppEngine

After a few struggles I finally managed to authenticate to Twitter using OAuth on AppEngine with Java. After trying out a few other options, I used oauth-signpost – a simple Java OAuth API. Although their desktop application sample worked with no surprises, once I tried the web application, I ran into 401s. The obvious problem was on my Twitter application settings page, I had forgotten to set my application type to ‘Browser’. Also, callback URL is required, so I had to use the right APP_ID and CALLBACK.

Twitter App Settings

Then I ran into another problem. In the desktop example, there is one instance of OAuthConsumer, and one instance of OAuthProvider, and they live happily within the main method block. But in the web app, the entire action is split into two parts. First, you have to retrieve a request token and redirect the user to the Twitter OAuth page:

There are two attributes stored in a session, so don’t forget to enable sessions in your appengine-web.xml (note that sessions are persisted in datastore):

Second, you need to retrieve the access token with a new request, processing the callback after successful Twitter authentication. But at this stage, if you don’t have that earlier created consumer and provider, you need to create them again. But not just create, you need to initialize them to the state they are after retrieving a request token. Simply said, you need to do this:

That’s it. Now, you can run your Twitter API queries and hopefully also enjoy announced and increased API rate limits for OAuth in the very near future.

This blog post was verified by korekt.me

This entry was posted in AppEngine, Twitter. Bookmark the permalink.

9 Responses to Twitter OAuth on Java AppEngine

  1. Parashuram says:

    Here is integrating twitter with Google App Engine using the Twitter4J. Twitter4J has direct APIs, makes interacting with Twitter easy.

    http://code.google.com/p/twitteybot/source/browse/trunk/src/com/appspot/twitteybot/ui/TwitterAccountManager.java

  2. Martin Adamek says:

    You have a very nice Twitter icon there :-) I looked at Twiiter4J but I prefer to handle all the Twitter API by myself.

  3. Pingback: uberVU - social comments

  4. Pingback: Tweets that mention Martin Adamek » Twitter OAuth on Java AppEngine -- Topsy.com

  5. Gustavo says:

    Martin

    My name is Gustavo and I work in Samsung SIDI, the brazilian inovation and research center of Samsung.

    We want to talk to you about this application, to know how it works, know your plans to, and some other things to discust about it.

    Is it possible?

    Thanks and best regards,

    Gustavo P. B. de Camargo

  6. Pingback: Blog sobre desenvolvimento de softwares em internet, banco de dados, programação e boas práticas, tecnologias e conceitos.

  7. will says:

    Thanks. Just what I needed. All the other samples assume the original consumer is still in scope – which is not the case when you’re doing this from a webapp.

  8. Justin Meyer says:

    After the server the access token:

    provider.retrieveAccessToken(consumer, oauth_verifier);

    Should you be storing that access token somewhere for later use? Thanks,

  9. Martin Adamek says:

    @Justin according to JavaDoc retrieveAccessToken() has void return type

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>