title: Twitter4j: java.lang.IllegalStateException: Access token already available
body: <p>I am using Twitter4j for creating Twitter client with JSP and servets. When I am requesting for access token I am getting the following exception:</p>
  
  <pre><code>java.lang.IllegalStateException: Access token already available.
  </code></pre>
  
  <p>Then I searched on Stack Overflow. I got <a href="https://stackoverflow.com/questions/12990031/twitter4j-access-token-already-available">this post</a> where in solution the author has written:</p>
  
  <blockquote>
    <p>I was setting an Access Token hard coded by the Configuration Builder.</p>
  </blockquote>
  
  <p>But they haven't mentioned how they fixed it.I am also not hardcoding access token. Here is my code</p>
  
  <pre><code> 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());
  </code></pre>
  
  <p>This is the stacktrace</p>
  
  <pre><code>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)
  </code></pre>