views:

1079

answers:

2

I am practicing making a web app which tries to read the user's twitter profile, display his/her friends and show his/her picture. The code example at the Twitter4j website goes:

public static void main(String args[]) thrwos Exception{
   Twitter twitter = new Twitter();
   twitter.setOAuthConsumer("[consumer key]", "[consumer secret]");
   RequestToken requestToken = twitter.getOAuthRequestToken();
   AccessToken accessToken = null;
   BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
   while (null == accessToken) {
        System.out.println("Open the following URL 
           and grant access to your account:");
        System.out.println(requestToken.getAuthorizationURL());
        System.out.print("Hit enter when it's done.[Enter]:");
        br.readLine();
        try{
          accessToken = requestToken.getAccessToken();
        } catch (TwitterException te) {
        if(401 == te.getStatusCode()){
        System.out.println("Unable to get the access token.");
        }else{
         te.printStackTrace();
       }
  }
 }
}

What I do is in a servlet, let's name it IndexServlet I have the following code:

   Twitter twitter = new Twitter();
   twitter.setOAuthConsumer("[consumer key]", "[consumer secret]");
   RequestToken requestToken = twitter.getOAuthRequestToken();
   String authUrl = requestToken.getAuthorizationURL()

and then I pass the authUrl to a jsp so the user can click on it. That works so far but after that I am already stuck! :)

I am not so sure where to set the callback URL. Should I set it to the same servlet? The problem with that is I should have the same twitter and requestToken variables just like the first code with main method so I can do this:

accessToken = requestToken.getAccessToken();
Status status = twitter.updateStatus(some argument here);

But setting the callback url to the IndexServlet calls the code that setups the twitter consumer. I could probably make a way around it using a flag like

if (already setup) {
   accessToken = requestToken.getAccessToken();
   Status status = twitter.updateStatus(some argument here);
}

but then I think that's messy.

What I've tried is (using a second servlet which is the callback url)

  1. I tried storing the twitter and requestToken variables in the session so I can access it when twitter redirects to the callback url but apparently it's not allowed.

  2. I also tried saving both twitter and request token to the datastore but the latter does not allow RequestToken types to be stored.

So apparently all have failed. Was wondering about the right way to do this. Thanks! :)

+1  A: 

You can store the data in the session. Google App Engine supports this, but you need to enable it.

NamshubWriter
My sessions are enabled in app engine. I tried that but to no avail. I am keeping record of my progress in this blog post: http://research2009.wordpress.com/2009/08/24/finding-a-web-solution-for-twitter4j-results-of-session-solution/
Jeune
+2  A: 

I worked out a hack to use Twitter4j + Oauth in google app engine. I blogged about it here.

Although this code works, it might not be the right way to use Twitter4j and Oauth (it does work on appengine though). It's not optimized, it’s definitely not the best way and, for the sake of brevity, it’s actually riddled with bad practices! In my real app though I have refactored those code smells already.

Another thing, I have yet to address is why Twitter’s icon shows up after it redirects to the to callback url. I intend to work on that soon as well as optimizing the method. Comments and suggestions are highly welcome!

Jeune