我正在使用Spring框架使用twitter登录我的网站,我实际上是在学习this tutorial。
所以我跟着this question并在下面更改了我的来源:
@RequestMapping("/welcome")
@Controller
public class HelloController {
private final String CONSUMER_KEY = "xxxx";
private final String CONSUMER_SECRET = "xxxx";
ConfigurationBuilder confBuilder = new ConfigurationBuilder()
.setOAuthConsumerKey(CONSUMER_KEY)
.setOAuthConsumerSecret(CONSUMER_SECRET);
Configuration configuration = confBuilder.build();
TwitterFactory factory = new TwitterFactory(configuration);
Twitter twitter = factory.getInstance();
@Autowired
private MyAccessToken accestoken;
@RequestMapping(method = RequestMethod.GET)
public String printWelcome(HttpServletResponse response,HttpServletRequest request) {
twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
RequestToken requestToken;
try {
String callbackURL = "http://127.0.0.1:8080/cwitter/callback";
System.out.println("callback url is " + callbackURL);
requestToken = twitter.getOAuthRequestToken(callbackURL);
String token = requestToken.getToken();
String tokenSecret = requestToken.getTokenSecret();
accestoken.setTokensecret(tokenSecret);
accestoken.setToken(token);
String authUrl = requestToken.getAuthorizationURL();
request.setAttribute("authUrl", authUrl);
} catch (TwitterException e) {
e.printStackTrace();
}
return "login";
}
@RequestMapping(value = "/{username}/test1",method=RequestMethod.GET)
public String test1(@PathVariable String username,@RequestParam("userdetails") String userDetails,ModelMap model){
System.out.println("test1 " + username + "\t" + userDetails);
model.addAttribute("message", "Spring 3 MVC Hello World");
return "hello";
}
@RequestMapping(value = "/callback",method=RequestMethod.GET)
public String test1(HttpServletRequest request, ModelMap model){
Twitter twitter = (Twitter) request.getSession().getAttribute("twitter");
System.out.println(twitter);
RequestToken requestToken = (RequestToken) request.getSession().getAttribute("requestToken");
String verifier = request.getParameter("oauth_verifier");
Enumeration attrList = request.getSession().getAttributeNames();
while(attrList.hasMoreElements()){
System.out.println(request.getSession().getAttribute((String)attrList.nextElement()));
}
try {
System.out.println(twitter.getOAuthAccessToken(requestToken, verifier));
request.getSession().removeAttribute("requestToken");
// twitter.setOAuthAccessToken(accessToken);
User user = twitter.verifyCredentials();
model.addAttribute("message", user.getName());
} catch (TwitterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "hello";
}
}
所以,我只是尝试运行我的所有项目,但它说了以下日志:
SEVERE: Servlet.service() for servlet [mvc-dispatcher] in context with path [/cwitter] threw exception [Request processing failed; nested exception is java.lang.IllegalStateException: consumer key/secret pair already set.] with root cause
java.lang.IllegalStateException: consumer key/secret pair already set.
at twitter4j.TwitterBaseImpl.setOAuthConsumer(TwitterBaseImpl.java:281)
at com.common.controller.HelloController.printWelcome(HelloController.java:51)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:175)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:446)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:434)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:221)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:107)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:76)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:934)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:90)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:515)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1012)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:642)
at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1597)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1555)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
请告诉我,这里有什么问题?
任何形式的帮助都会受到赞赏!!