3
我想在我的appengine項目中使用twitter4j來獲取用戶的訪問令牌。我的代碼如下twitter4j getOauthAccessToken給出異常
HttpSession session = req.getSession();
String tokenCopy = (String) session
.getAttribute(Constants.TOKEN_SESSION_KEY);
String tokenSecretCopy = (String) session
.getAttribute(Constants.TOKENSECRET_SESSION_KEY);
String oauth_token = req.getParameter("oauth_token");
//The above is the same as the tokenCopy, verified by printing.
String oauth_verifier = req.getParameter("oauth_verifier");
Twitter twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(Constants.CONSUMER_KEY,
Constants.CONSUMER_SECRET);
String extraHTML = "NOTHING\n";
try {
AccessToken accessToken;
accessToken = twitter
.getOAuthAccessToken(tokenCopy, oauth_verifier);
// accessToken = twitter.getOAuthAccessToken(oauth_verifier);
// accessToken = twitter.getOAuthAccessToken();
String accessTokenStr = accessToken.getToken();
String accessTokenSecret = accessToken.getTokenSecret();
extraHTML = "<p>accessToken = " + accessTokenStr
+ ", accessTokenSecret = " + accessTokenSecret + "</p>\n";
// TODO latertwitter.setOAuthAccessToken(accessToken);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
extraHTML = "<pre>" + e.getMessage() + "</pre>\n";
}
可悲的是,我的代碼運行時,我總是異常
401:Authentication credentials (https://dev.twitter.com/docs/auth) were missing or
incorrect. Ensure that you have set valid conumer key/secret, access token/secret,
and the system clock in in sync.
我知道我的CONSUMER_KEY
和CONSUMER_SECRET
是正確的(我用它們來創建Twitter的驗證網址和效果很好)。我在這裏錯過了什麼?我是否應該通過別的方式致電getOAuthAccessToken()
?
謝謝。
非常感謝!這工作。該程序實際上是錯誤地使用了'getOAuthAccessToken(java.lang.String,java.lang.String)'方法。 – Animesh