protected void doGet(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException {
TwitterFactory twitterFac = new TwitterFactory();
Twitter twitter = twitterFac.getInstance();
twitter.setOAuthConsumer("key", "secret");
request.getSession().setAttribute("twitter", twitter);
try
{
RequestToken requestToken = twitter.getOAuthRequestToken("http://local.your-domain.com:8080/sign-in-with-twitter/callback");
request.getSession().setAttribute("requestToken", requestToken);
System.out.println(requestToken.getAuthenticationURL());
response.sendRedirect(requestToken.getAuthenticationURL());
} catch (TwitterException e)
{
throw new ServletException(e);
}
}
}
****然后Twitter页面重定向到我的应用程序页面(CallbackServlet)。所有这些都是正确执行的。****
然后CallbackServlet具有以下doGet方法:
public class CallbackServlet扩展HttpServlet { private static final long serialVersionUID = 1657390011452788111L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Twitter twitter = (Twitter) request.getSession().getAttribute("twitter");
RequestToken requestToken = (RequestToken) request.getSession().getAttribute("oauth_token");
String verifier = request.getParameter("oauth_verifier");
try {
twitter.getOAuthAccessToken(requestToken, verifier);
request.getSession().removeAttribute("requestToken");
} catch (TwitterException e) {
throw new ServletException(e);
}
response.sendRedirect(request.getContextPath() + "/");
}
}
============================= CallbackServlet尝试将login和requestToken保存在LoginServlet中(使用方法getSession()。getAttribute())。这就是问题所在:twitter对象总是为空!
plz排序