Xiaoke's Blog

未觉池塘春草梦,阶前梧叶已秋声

在Servlet中使用Twitter OAuth认证

例子来自Twitter4j的作者,我自己的代码等完善了再发布:

主要原理是在Twitter验证完毕后重定向到Callback的网址时,获取网址后面的oauth_verifier参数,进而获取AccessToken,并存储供以后使用。

SigninServlet

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package twitter4j.examples.signin;

import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.http.RequestToken;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class SigninServlet extends HttpServlet {
private static final long serialVersionUID = -6205814293093350242L;

protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
Twitter twitter = new Twitter();
request.getSession().setAttribute("twitter", twitter);
try {
StringBuffer callbackURL = request.getRequestURL();
int index = callbackURL.lastIndexOf("/");
callbackURL.replace(index, callbackURL.length(),
"").append("/callback");

RequestToken requestToken =
twitter.getOAuthRequestToken(callbackURL.toString());
request.getSession().setAttribute("requestToken", requestToken);
response.sendRedirect(requestToken.getAuthenticationURL());

} catch (TwitterException e) {
throw new ServletException(e);
}

}
}

CallbackServlet

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package twitter4j.examples.signin;

import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.http.RequestToken;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class CallbackServlet extends 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("requestToken");
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()+ "/");
}
}

mcxiaoke

A Android/Java/Python developer and entrepreneur. Spends his time travelling the world with a bag of kites. Likes books and movies.

Comments