Skip to content

Instantly share code, notes, and snippets.

@ts-3156
Created March 6, 2011 10:07
Show Gist options
  • Save ts-3156/857180 to your computer and use it in GitHub Desktop.
Save ts-3156/857180 to your computer and use it in GitHub Desktop.
package getAccessToken;
import java.io.IOException;
import java.util.logging.Logger;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.auth.AccessToken;
import twitter4j.auth.RequestToken;
@SuppressWarnings("serial")
public class CallbackServlet extends HttpServlet {
private static final Logger log = Logger.getLogger(CallbackServlet.class.getName());
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
RequestToken requestToken = (RequestToken) req.getSession().getAttribute("requestToken");
Twitter twitter = (Twitter) req.getSession().getAttribute("twitter");
String verifier = req.getParameter("oauth_verifier");
AccessToken accessToken = null;
try {
accessToken = twitter.getOAuthAccessToken(requestToken, verifier);
} catch (TwitterException e) {
log.warning(e.getMessage());
}
if (accessToken != null) {
// アクセストークンを永続化する
}
req.getSession().removeAttribute("requestToken");
resp.sendRedirect(req.getContextPath() + "/");
}
}
getAccessToken.GetAccessTokenServlet doPost: 401:Authentication credentials (http://dev.twitter.com/pages/auth) were missing or incorrect. Ensure that you have set valid conumer key/secret, access token/secret, and the system clock in in sync.
<?xml version="1.0" encoding="UTF-8"?>
<hash>
<request>/oauth/request_token</request>
<error>Desktop applications only support the oauth_callback value 'oob'</error>
</hash>
package getAccessToken;
import java.io.IOException;
import java.util.logging.Logger;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.RequestToken;
@SuppressWarnings("serial")
public class GetAccessTokenServlet extends HttpServlet {
private static final Logger log = Logger.getLogger(GetAccessTokenServlet.class.getName());
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
StringBuffer callbackURL = req.getRequestURL();
int index = callbackURL.lastIndexOf("/");
callbackURL.replace(index, callbackURL.length(), "").append("/callback");
try {
Twitter twitter = new TwitterFactory().getInstance();
// ここでtwitter.setOAuthConsumer("consumerKey", "consumerSecret")を呼ぶとIllegalStateExceptionになるから
// この二つはちゃんとセットされているっぽい
RequestToken requestToken = twitter.getOAuthRequestToken(callbackURL.toString());
// たぶん、↑ここで失敗してる。このページから遷移しない。
req.getSession().setAttribute("requestToken", requestToken);
req.getSession().setAttribute("twitter", twitter);
resp.sendRedirect(requestToken.getAuthenticationURL());
} catch (TwitterException e) {
log.warning(e.getMessage());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment