Twitter4j: java.lang.IllegalStateException: Access token already available -
i using twitter4j creating twitter client jsp , servets. when requesting access token getting following exception.
java.lang.illegalstateexception: access token available.
then searched on stackoverflow. got post twitter4j access token available
where in solution has written
i setting access token hard coded configuration builder.
but haven't mentioned how has fixed it.i not hardcoding accesstoken here 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 " + requesttoken); request.getsession().setattribute("requesttoken", requesttoken); system.out.println(requesttoken.getauthenticationurl()); response.sendredirect(requesttoken.getauthenticationurl());
this stacktrace
http error 500 problem accessing /demo1. reason: access token available. caused by: java.lang.illegalstateexception: access token available. @ twitter4j.auth.oauthauthorization.getoauthrequesttoken(oauthauthorization.java:113) @ twitter4j.auth.oauthauthorization.getoauthrequesttoken(oauthauthorization.java:104) @ twitter4j.twitterbaseimpl.getoauthrequesttoken(twitterbaseimpl.java:281) @ com.example.demo1.doget(demo1.java:69) @ javax.servlet.http.httpservlet.service(httpservlet.java:707) @ javax.servlet.http.httpservlet.service(httpservlet.java:820) @ org.eclipse.jetty.servlet.servletholder.handle(servletholder.java:565) @ org.eclipse.jetty.servlet.servlethandler.dohandle(servlethandler.java:479) @ org.eclipse.jetty.server.handler.scopedhandler.handle(scopedhandler.java:119) @ org.eclipse.jetty.security.securityhandler.handle(securityhandler.java:521) @ org.eclipse.jetty.server.session.sessionhandler.dohandle(sessionhandler.java:227) @ org.eclipse.jetty.server.handler.contexthandler.dohandle(contexthandler.java:1031) @ org.eclipse.jetty.servlet.servlethandler.doscope(servlethandler.java:406) @ org.eclipse.jetty.server.session.sessionhandler.doscope(sessionhandler.java:186) @ org.eclipse.jetty.server.handler.contexthandler.doscope(contexthandler.java:965) @ org.eclipse.jetty.server.handler.scopedhandler.handle(scopedhandler.java:117) @ org.eclipse.jetty.server.handler.handlerwrapper.handle(handlerwrapper.java:111) @ org.eclipse.jetty.server.server.handle(server.java:349) @ org.eclipse.jetty.server.abstracthttpconnection.handlerequest(abstracthttpconnection.java:449) @ org.eclipse.jetty.server.abstracthttpconnection$requesthandler.headercomplete(abstracthttpconnection.java:910) @ org.eclipse.jetty.http.httpparser.parsenext(httpparser.java:634) @ org.eclipse.jetty.http.httpparser.parseavailable(httpparser.java:230) @ org.eclipse.jetty.server.asynchttpconnection.handle(asynchttpconnection.java:76) @ org.eclipse.jetty.io.nio.selectchannelendpoint.handle(selectchannelendpoint.java:609) @ org.eclipse.jetty.io.nio.selectchannelendpoint$1.run(selectchannelendpoint.java:45) @ org.eclipse.jetty.util.thread.queuedthreadpool.runjob(queuedthreadpool.java:599) @ org.eclipse.jetty.util.thread.queuedthreadpool$3.run(queuedthreadpool.java:534) @ java.lang.thread.run(thread.java:679)
please take if can help.
i met similar problem , debug source code line line. found configuration take multiple sources before ready. when setting properties such consumerkey, consumersecret, set these lines
configurationbuilder cb = new configurationbuilder(); cb.setdebugenabled(true) .setoauthconsumerkey(getservletcontext().getinitparameter("consumerkey")) .setoauthconsumersecret(getservletcontext().getinitparameter("consumersecret"));
in addition, twitter4j scan classpath , find property files defines related key value pairs. created file following instruction of tutorial , forget delete this. in tutorial, told how access api generated accesstoken , accesssecret, cause of illegalstatusexception.
after found issue, , deleted accesstoken , accesstokensecret keys in property file. problem solved.
hope can you.
Comments
Post a Comment