我尝试使用twitter4j对Twitter进行身份验证时出现问题。我已经尝试了this,但它仍然无效。
这是我的代码,非常感谢任何帮助。
感谢。
public class SpeedDemon {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws TwitterException {
// Setup for Snake Charmer
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey("CONSUMER_KEY")
.setOAuthConsumerSecret("CONSUMER_SECRET")
.setOAuthAccessToken("OAUTH_ACCESS")
.setOAuthAccessTokenSecret("OAUTH_SECRET");
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
// Gets timeline.
Twitter twit = TwitterFactory.getSingleton();
List<Status> statuses = twit.getHomeTimeline();
System.out.println("Showing home timeline.");
for (Status status : statuses) {
System.out.println(status.getUser().getName() + ":" +
status.getText());
}
}
}
编辑:编译时发生以下错误:
Exception in thread "main" java.lang.IllegalStateException: Authentication credentials are missing. See http://twitter4j.org/en/configuration.html for details
at twitter4j.TwitterBaseImpl.ensureAuthorizationEnabled(TwitterBaseImpl.java:215)
at twitter4j.TwitterImpl.get(TwitterImpl.java:1784)
at twitter4j.TwitterImpl.getHomeTimeline(TwitterImpl.java:105)
at speeddemon.SpeedDemon.main(SpeedDemon.java:30)
C:\Users\Kevin\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)
答案 0 :(得分:1)
看起来你构建了一个Twitter实例两次,一次使用你构建的TwitterFactory,一次使用Singleton(我怀疑它没有auth设置)。
然后使用第二个Twitter实例(使用未经身份验证的工厂创建)来进行查询。
尝试使用twitter.getHomeTimeline()
而不是twit.getHomeTimeline()