2009-09-22 93 views
0

我正在GAE/J上使用Twitter4J撰写Twitter网络应用程序。Google App Engine中的HttpSession问题/ J

我在会话中保存Twitter和请求令牌对象,以便在回拨后使用。

我有两个servlet。 IndexServlet设置会话和HomeServlet从会话中获取(通过twitter oAuth回呼)。

如果我在两个servlet中注释掉会话处理行,那么回调函数可以正常工作。

请提出任何解决方法。我在这里分享我的代码。

IndexServlet.java

Twitter twitter = new Twitter(); 
       twitter.setOAuthConsumer("<masked>", "<masked>"); 
       RequestToken requestToken = null; 
      try { 
        requestToken = twitter.getOAuthRequestToken(); 
        log.info("OAuth token has been taken"); 
      } catch (TwitterException e) { 
        log.warning(e.toString()); 
      } 

      HttpSession session = request.getSession(); 
      if (session.getAttribute("twitter")==null){ 
        session.setAttribute("twitter", twitter); 
        out.println("-----------------------------> session is set"); 
      } 

      if (session.getAttribute("token")==null){ 
        session.setAttribute("token", requestToken); 
        out.println("-----------------------------> session is set"); 
      } 
      String authUrl = requestToken.getAuthorizationURL(); 

HomeServlet.java

     HttpSession session = request.getSession(); 

        twitter = (Twitter)session.getAttribute("twitter"); 
        r = (RequestToken)session.getAttribute("token"); 

        twitter.setOAuthAccessToken(r.getAccessToken()); 

        twitter.updateStatus("Hello World!"); 

异常

javax.servlet.ServletException: java.lang.ArrayStoreException: [Ljava.lang.String; 
at com.google.apphosting.runtime.jetty.AppVersionHandlerMap.handle(AppVersionHandlerMap.java:239) 
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:139) 
at org.mortbay.jetty.Server.handle(Server.java:313) 
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:506) 
at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:830) 
at com.google.apphosting.runtime.jetty.RpcRequestParser.parseAvailable(RpcRequestParser.java:76) 
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:381) 
at com.google.apphosting.runtime.jetty.JettyServletEngineAdapter.serviceRequest(JettyServletEngineAdapter.java:139) 
at com.google.apphosting.runtime.JavaRuntime.handleRequest(JavaRuntime.java:235) 
at com.google.apphosting.base.RuntimePb$EvaluationRuntime$6.handleBlockingRequest(RuntimePb.java:4950) 
at com.google.apphosting.base.RuntimePb$EvaluationRuntime$6.handleBlockingRequest(RuntimePb.java:4948) 
at com.google.net.rpc.impl.BlockingApplicationHandler.handleRequest(BlockingApplicationHandler.java:24) 
at com.google.net.rpc.impl.RpcUtil.runRpcInApplication(RpcUtil.java:359) 
at com.google.net.rpc.impl.Server$2.run(Server.java:823) 
at com.google.tracing.LocalTraceSpanRunnable.run(LocalTraceSpanRunnable.java:56) 

回答

1

如果在AppEngine上的会话设置为true,不工作,然后得到的requestToken令牌和tokenSecret

Twitter twitter = new Twitter(); 
twitter.setOAuthConsumer(consumerKey,consumerSecret); 
RequestToken requestToken = twitter.getOAuthRequestToken(); 

String token = requestToken.getToken(); 
String tokenSecret = requestToken.getTokenSecret(); 

HttpSession session = request.getSession(); 
session.setAttribute("token",token); 
session.setAttribute("tokenSecret",tokenSecret); 

在你HomeServlet从会话中检索令牌和tokenSecret:

Twitter twitter = new Twitter(); 
twitter.setOAuthConsumer(consumerKey,consumerSecret); 
AccessToken accessToken = 
    twitter.getOAuthAccessToken(token, tokenSecret); 
twitter.setOAuthAccessToken(accessToken); 

我也尝试过存储twitter和requestToken对象,但它没有为我工作。但是我刚才向你展示的这段代码确实如此我有一个教程BTW张贴在这里:http://jeungun.wordpress.com/2009/09/03/quick-and-dirty-twitter4j-oauth-for-web-apps/

0

reche CK您在得到

<appengine-web-app xmlns="http://appengine.google.com/ns/1.0"> 
... 
    <sessions-enabled>true</sessions-enabled> 
... 
</appengine-web-app> 

你的战争/ WEB-INF/lib目录/ AppEngine上-web.xml中

+0

是的,我忘了提,但我已经在appengin XML文件中启用它。 – 2009-09-23 06:44:51