我有这个代码
PrintWriter out = response.getWriter();
String username="twitterapi";
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey("zRQs******yDLJuQ")
.setOAuthConsumerSecret("W368******************DsWya56uk")
.setOAuthAccessToken("124529**************EQ2XAfAQShzSWExwMoOS")
.setOAuthAccessToken("UiH**********************k6UuCPveirdF");
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
User user = null;
try {
user = twitter.showUser(username);
} catch (TwitterException ex) {
Logger.getLogger(Twitter_Final.class.getName()).log(Level.SEVERE, null, ex);
}
if (user.getStatus() != null) {
out.println("@" + user.getScreenName() + " - " + user.getDescription());
} else {
// protected account
out.println("@" + user.getScreenName());
}
但是我得到了众所周知的错误消息“缺少身份验证凭据。有关详细信息,请参见http://twitter4j.org/en/configuration.html。”
我在这里读过类似的问题,但是我看不到代码中的问题在哪里。你能?
最佳答案
问题出在您的ConfigurationBuilder
中,您两次设置了OAuthAccessToken
,但从未设置OAuthAccessTokenSecret
:
只需将代码更改为:
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey(YOUR_KEY)
.setOAuthConsumerSecret(YOUR_SECRET)
.setOAuthAccessToken(ACCESS_TOKEN)
.setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET);
会的!