1

我正在使用 Twitter4j 创建带有 JSP 和服务器的 Twitter 客户端。当我请求访问令牌时,出现以下异常:

java.lang.IllegalStateException: Access token already available.

然后我在 Stack Overflow 上搜索。我得到了这篇文章,作者在解决方案中写道:

我正在设置由配置生成器硬编码的访问令牌。

但他们没有提到他们是如何修复它的。我也没有硬编码访问令牌。这是我的代码

 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)
4

2 回答 2

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 键。然后问题解决了。

希望这可以帮到你。

于 2013-07-17T06:25:39.497 回答
0

我遇到了同样的问题,那是因为配置生成器中已经提供了访问令牌。所以你需要在配置生成器中将它设置为 null

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((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());

使用此代码能够正确运行程序。

于 2016-07-18T05:42:09.470 回答