【问题标题】:Unable to authenticate using twitter4j无法使用 twitter4j 进行身份验证
【发布时间】:2016-04-29 15:00:58
【问题描述】:

我在尝试使用 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)

【问题讨论】:

    标签: java api twitter twitter4j


    【解决方案1】:

    看起来您构造了两次 Twitter 实例,一次使用您构造的 TwitterFactory,一次使用 Singleton(我怀疑它没有身份验证设置)。

    然后您使用第二个 Twitter 实例(使用未经身份验证的工厂创建)进行查询。

    尝试使用twitter.getHomeTimeline() 而不是twit.getHomeTimeline()

    【讨论】:

    • 就是这样!非常感谢!
    猜你喜欢
    • 2020-05-15
    • 1970-01-01
    • 1970-01-01
    • 2013-05-30
    • 1970-01-01
    • 1970-01-01
    • 2016-01-11
    • 2016-10-11
    • 1970-01-01
    相关资源
    最近更新 更多