@RequestMapping("/twitter")
public ModelAndView twitter(HttpServletRequest request) throws Exception {
String ConsumerKey="WZIBZVr1H6nst0D**********";
String ConsumerSecret="chj82ljAXNmO544cUAEkSNDon3eTm3Bn200qOEK7**********";
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey(ConsumerKey)
.setOAuthConsumerSecret(ConsumerSecret);
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
RequestToken requestToken = twitter.getOAuthRequestToken();
request.getSession().setAttribute("userToken",requestToken.getToken());
request.getSession().setAttribute("userSecretToken",requestToken.getTokenSecret());
System.out.println("requestToken.getToken() ====> "+requestToken.getToken());
System.out.println("requestToken.getTokenSecret() ====> "+requestToken.getTokenSecret());
return new ModelAndView("redirect:"+requestToken.getAuthorizationURL());
}
@RequestMapping("/favList")
public String favList(HttpServletRequest request,ModelMap model) throws Exception {
String ConsumerKey="WZIBZVr1H6nst0D**********";
String ConsumerSecret="chj82ljAXNmO544cUAEkSNDon3eTm3Bn200qOEK7**********";
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey(ConsumerKey)
.setOAuthConsumerSecret(ConsumerSecret);
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
String userToken = (String)request.getSession().getAttribute("userToken");
String oauthToken = (String)request.getParameter("oauth_token");
String oauthVerifier = (String)request.getParameter("oauth_verifier");
System.out.println("oauthToken ====> "+oauthToken);
System.out.println("oauthVerifier ====> "+oauthVerifier);
AccessToken accessToken=null;
if(userToken =="" || userToken ==null){
return "redirect:/twitter.do";
}
if(userToken.equals(oauthToken)){
try{
accessToken= twitter.getOAuthAccessToken(oauthToken,oauthVerifier);
}catch(TwitterException e){
}
twitter.setOAuthAccessToken(accessToken);
List<Status> statuses =null;
Paging page = new Paging();
page.count(20);
page.setPage(1);
List<String> favList =new ArrayList<String>();
statuses =twitter.getHomeTimeline(); <----- error line
for(Status status: statuses){
favList.add("@"+status.getUser().getScreenName() + " - " + status.getText());
}
model.addAttribute("favList",favList);
}
return "/web/favList";
}
=============================================== ================================================== ====
错误:servlet [spring-web]的Servlet.service()与path []的上下文引发了异常[请求处理失败;嵌套异常是java.lang.IllegalStateException:缺少身份验证凭据。有根本原因,请参阅http://twitter4j.org/en/configuration.html了解详细信息 java.lang.IllegalStateException:缺少身份验证凭据。有关详细信息,请参阅http://twitter4j.org/en/configuration.html
在twitter4j.TwitterBaseImpl.ensureAuthorizationEnabled(TwitterBaseImpl.java:186) 在twitter4j.TwitterImpl.get(TwitterImpl.java:1554) 在twitter4j.TwitterImpl.getHomeTimeline(TwitterImpl.java:105) 在web.controller.twitterTest.favList(twitterTest.java:113) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) 在org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176) 在org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:440) 在org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:428) 在org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925) 在org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856) 在org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936) 在org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827) 在javax.servlet.http.HttpServlet.service(HttpServlet.java:621) 在org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812) 在javax.servlet.http.HttpServlet.service(HttpServlet.java:728) 在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) 在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 在org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51) 在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) 在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 在org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88) 在org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) 在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 在org.apache.logging.log4j.core.web.Log4jServletFilter.doFilter(Log4jServletFilter.java:66) 在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) 在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) 在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) 在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) 在org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953) 在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) 在org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041) at org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:603) at org.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java:312) 在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:615) 在java.lang.Thread.run(Thread.java:745)