Search code examples
google-app-enginetwitter-oauthtwitter4j

Twitter oAuth redirects to 404, but only the first time


Using twitter4j v3.0.3, every aspect of Twitter integration works, except that when authenticating/authorizing my, the browser is redirected to http://api.twitter.com/login which returns "Sorry, that page doesn’t exist!"

This only happens THE FIRST TIME, i.e.browser with a clear cache or an Incognito window. My server reports that it's redirecting to this URL...

http://api.twitter.com/oauth/authenticate?oauth_token=PTlVt6aisFy7UytjsRM5poFHcdGEjGtgNpxhJ8UbQ

...the browser confirms a 302 to...

https://api.twitter.com/oauth/authenticate;jsessionid=pw65se84inj9?oauth_token=PTlVt6aisFy7UytjsRM5poFHcdGEjGtgNpxhJ8UbQ

...and that request generates a 302 to the page which does not exist, i.e.

https://api.twitter.com/login?redirect_after_login=%2Foauth%2Fauthenticate%3Bjsessionid%3Dpw65se84inj9%3Foauth_token%3DPTlVt6aisFy7UytjsRM5poFHcdGEjGtgNpxhJ8UbQ

When the user navigates back from the "Sorry, that page doesn’t exist!" page and then retries, the oauth flow works perfectly.

I'm guessing this is something to do with the appended jsessionid?

This issue is on local development server, but the 404 has also been seen intermittently on GAE production.


Solution

  • I found the solution here: https://stackoverflow.com/a/19690688/2698327

    You identified correctly the problem: GAE append a jsessionid parameter for the first request made by a user (i.e. when the session is created).

    To prevent it from doing so you can edit the web.xml and append the following:

    <context-param>
        <param-name>org.mortbay.jetty.servlet.SessionURL</param-name>
        <param-value>none</param-value>
    </context-param>