简体   繁体   English

Twitter身份验证错误“身份验证凭据丢失”

[英]Twitter authentication error “Authentication credentials are missing”

I have the following code for using the Twitter API in Java 我具有以下代码,用于在Java中使用Twitter API

import java.io.File;
import java.io.PrintWriter;
import java.util.List;
import twitter4j.Query;
import twitter4j.QueryResult;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterFactory;
import twitter4j.conf.ConfigurationBuilder;

public class TwitterDataCollector {

    public static void main(String[] args) throws Exception {

        ConfigurationBuilder cb = new ConfigurationBuilder();

        cb.setDebugEnabled(true)
          .setOAuthConsumerKey(comsumerKey)
          .setOAuthConsumerSecret(consumerSecret)
          .setOAuthAccessToken(accesstoken)
          .setOAuthAccessTokenSecret(accesstokensecret);

        TwitterFactory tf = new TwitterFactory(cb.build());
        Twitter twitter = tf.getInstance();

        //PrintWriter tweetWriter = null;
         twitter = TwitterFactory.getSingleton();
        List<Status> statuses = twitter.getHomeTimeline();
        System.out.println("Showing home timeline.");

        for (Status status : statuses) {
            System.out.println(status.getUser().getName() + ":" + status.getText());
        }
    }
}

I am trying to authenticate to the Twitter API with consumer key and access token and trying to access my timeline but it is showing 我正在尝试使用使用者密钥和访问令牌对Twitter API进行身份验证,并尝试访问我的时间轴,但它正在显示

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:186)
at twitter4j.TwitterImpl.get(TwitterImpl.java:1554)
at twitter4j.TwitterImpl.getHomeTimeline(TwitterImpl.java:105)
at TwitterDataCollector.main(TwitterDataCollector.java:24)

The removal of the line 拆除线

twitter = TwitterFactory.getSingleton();

Resolves your issue; 解决您的问题; getInstance() is returning you a Twitter using the auth details you specified in the ConfigurationBuilder . getInstance()使用在ConfigurationBuilder指定的身份验证详细信息向您返回Twitter

If you want to use getSingleton() you will need to do some priming of the ConfigurationContext with your auth details. 如果要使用getSingleton() ,则需要使用身份验证详细信息对ConfigurationContext进行一些getSingleton() I'd hope that there is some documentation on how this should be done 我希望有一些有关如何完成此操作的文档

On a side note I would reccommend not posting your OAuth keys to a public website 另外,我建议不要将您的OAuth密钥发布到公共网站上

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM