我使用Twitter4j用JSP和servets创建Twitter客户端。当我请求访问令牌时,我收到以下异常:
java.lang.IllegalStateException: Access token already available.
然后我搜索了Stack Overflow。我得到了this post作者写的解决方案:
我正在设置由配置构建器硬编码的访问令牌。
但他们没有提到他们如何修复它。我也没有硬编码访问令牌。这是我的代码
StringBuffer callbackURL = request.getRequestURL();
System.out.println("callbackurl is" + callbackURL);
int index = callbackURL.lastIndexOf("/");
callbackURL.replace(index, callbackURL.length(), "").append("/callback");
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey(getServletContext().getInitParameter("consumerKey"))
.setOAuthConsumerSecret(getServletContext().getInitParameter("consumerSecret"));
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
System.out.println("Twitter is" + twitter);
request.getSession().setAttribute("twitter", twitter);
RequestToken requestToken = twitter.getOAuthRequestToken(callbackURL.toString());
System.out.println("request token is " + requestToken);
request.getSession().setAttribute("requestToken", requestToken);
System.out.println(requestToken.getAuthenticationURL());
response.sendRedirect(requestToken.getAuthenticationURL());
这是堆栈跟踪
HTTP ERROR 500
Problem accessing /Demo1. Reason:
Access token already available.
Caused by:
java.lang.IllegalStateException: Access token already available.
at twitter4j.auth.OAuthAuthorization.getOAuthRequestToken(OAuthAuthorization.java:113)
at twitter4j.auth.OAuthAuthorization.getOAuthRequestToken(OAuthAuthorization.java:104)
at twitter4j.TwitterBaseImpl.getOAuthRequestToken(TwitterBaseImpl.java:281)
at com.example.Demo1.doGet(Demo1.java:69)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:565)
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:479)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:119)
at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:521)
at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:227)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1031)
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:406)
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:186)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:965)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:117)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:111)
at org.eclipse.jetty.server.Server.handle(Server.java:349)
at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:449)
at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.headerComplete(AbstractHttpConnection.java:910)
at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:634)
at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:230)
at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:76)
at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:609)
at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:45)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:599)
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:534)
at java.lang.Thread.run(Thread.java:679)
答案 0 :(得分:0)
我最近遇到了类似的问题,我逐行调试源代码。我终于发现配置在准备好之前会占用多个源。设置诸如consumerKey,consumerSecret之类的属性时,它将从这些行设置
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey(getServletContext().getInitParameter("consumerKey"))
.setOAuthConsumerSecret(getServletContext().getInitParameter("consumerSecret"));
此外,twitter4j将扫描类路径并找到定义相关键值对的属性文件。我按照教程的说明创建了这个文件,但我忘了删除它。但是在教程中,它只是告诉你如何使用生成的accessToken和accessSecret来访问api,这是导致IllegalStatusException的原因。
发现此问题后,我删除了属性文件中的accessToken和accessTokenSecret键。然后问题解决了。
希望这可以帮到你。
答案 1 :(得分:0)
我遇到了同样的问题,那是因为配置构建器中已经存在访问令牌。所以你需要在配置构建器
中将其设置为nullStringBuffer callbackURL = request.getRequestURL();
System.out.println("callbackurl is" + callbackURL);
int index = callbackURL.lastIndexOf("/");
callbackURL.replace(index, callbackURL.length(), "").append("/callback");
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true).setOAuthConsumerKey((String) "consumerkey")
.setOAuthConsumerSecret("consumersecret").setOAuthAccessToken(null)
.setOAuthAccessTokenSecret(null)
.setOAuthRequestTokenURL("https://api.twitter.com/oauth/request_token")
.setOAuthAuthorizationURL("https://api.twitter.com/oauth/authorize")
.setOAuthAccessTokenURL("https://api.twitter.com/oauth/access_token");
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
System.out.println("Twitter is" + twitter);
request.getSession().setAttribute("twitter", twitter);
RequestToken requestToken = twitter.getOAuthRequestToken(callbackURL.toString());
System.out.println("request token is " + requestToken);
request.getSession().setAttribute("requestToken", requestToken);
System.out.println(requestToken.getAuthenticationURL());
response.sendRedirect(requestToken.getAuthenticationURL());
使用此代码可以正确运行程序。