【问题标题】:Twitter4j authentication credentials are missing缺少 Twitter4j 身份验证凭据
【发布时间】:2014-01-05 10:52:57
【问题描述】:

我想在我的 Android 应用中使用 Twitter4j 发布推文。这是我的代码:

 //TWITTER SHARE.
@Click (R.id. img_btn_twitter)
@Background
public void twitterPostWall(){

    try {


        //Twitter Conf.
        ConfigurationBuilder cb = new ConfigurationBuilder();
        cb.setDebugEnabled(true)
                .setOAuthConsumerKey(CONSUMER_KEY)
                .setOAuthConsumerSecret(CONSUMER_SECRET)
                .setOAuthAccessToken(ACCESS_KEY)
                .setOAuthAccessTokenSecret(ACCESS_SECRET);

        TwitterFactory tf = new TwitterFactory(cb.build());
        Twitter twitter = new TwitterFactory().getInstance();
        twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);

        try {
            RequestToken requestToken = twitter.getOAuthRequestToken();

            Log.e("Request token: ", "" + requestToken.getToken());
            Log.e("Request token secret: ", "" + requestToken.getTokenSecret());
            AccessToken accessToken = null;


        }

        catch (IllegalStateException ie) {

            if (!twitter.getAuthorization().isEnabled()) {
                Log.e("OAuth consumer key/secret is not set.", "");
            }
        }


        Status status = twitter.updateStatus(postLink);
        Log.e("Successfully updated the status to [", ""  + status.getText() + "].");

    }

    catch (TwitterException te) {
        Log.e("TWEET FAILED", "");

    }
}

我总是从 Twitter4j 收到此错误消息:java.lang.IllegalStateException:缺少身份验证凭据。有关详细信息,请参阅http://twitter4j.org/en/configuration.html。但正如你所看到的,我正在使用 builder 来设置我的密钥。有人可以帮我解决它吗?谢谢。

【问题讨论】:

  • 你从不使用 TwitterFactory 实例 'tf' ...
  • 是的,但我不认为这是这个问题的重点。
  • 嗯,有点像,因为你说'如你所见,我正在使用构建器来设置我的密钥',但是使用构建器的唯一对象是'tf',它永远不会用过的。因此,在 builder 上设置凭据的效果为零。您需要使用以下命令创建 'twitter' 对象:Twitter twitter = tf.getInstance(); 并放弃对twitter.setOAuthConsumer的调用

标签: java android api twitter twitter4j


【解决方案1】:

问题如下。

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

您将配置传递给一个TwitterFactory 实例并使用另一个TwitterFactory 实例来获取Twitter 实例。

因此,你得到 java.lang.IllegalStateException: Authentication credentials are missing

我建议你修改你的代码如下:

    //Twitter Conf.
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true)
            .setOAuthConsumerKey(CONSUMER_KEY)
            .setOAuthConsumerSecret(CONSUMER_SECRET)
            .setOAuthAccessToken(ACCESS_KEY)
            .setOAuthAccessTokenSecret(ACCESS_SECRET);

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

并使用这个 twitter 实例。它会起作用的。

【讨论】:

    【解决方案2】:

    我在 Twitter4j 上的配置有问题,因为我没有提供正确的配置。所以为了修复它,我创建了以下函数来建立我的配置,以便以后在另一个函数中使用:

    public static void main(String args[]) throws Exception {
    
        TwitterServiceImpl impl = new TwitterServiceImpl();
        ResponseList<Status> resList = impl.getUserTimeLine("spacex");
    
        for (Status status : resList) {
            System.out.println(status.getCreatedAt() + ": " + status.getText());
        }
    }
    
    public ResponseList<Status> getUserTimeLine(String screenName) throws TwitterException {
        
        TwitterFactory twitterFactory = new TwitterFactory(getConfiguration().build());
        Twitter twitter = twitterFactory.getInstance();
        twitter.getAuthorization();
        Paging paging = new Paging(1, 10);
        twitter.getId();
        return twitter.getUserTimeline(screenName, paging);
    }
    
    public ConfigurationBuilder getConfiguration() {
        ConfigurationBuilder cb = new ConfigurationBuilder();
        cb.setDebugEnabled(true)
        .setOAuthConsumerKey("myConsumerKey")
        .setOAuthConsumerSecret("myConsumerSecret")
        .setOAuthAccessToken("myAccessToken")
        .setOAuthAccessTokenSecret("myAccessTokenSecret");
        return cb;
    }
    

    要获取所需信息,您必须拥有 Twitter 开发者帐户,要获取之前创建的应用的身份验证信息,请访问:Projects and Apps

    最后,我能够从 SpaceX 帐户中检索数据:

    Tue Nov 24 20:58:13 CST 2020: Falcon 9 launches Starlink to orbit – the seventh launch and landing of this booster https://twitter.com/SpaceX/status/1331431972430700545
    Tue Nov 24 20:29:36 CST 2020: Deployment of 60 Starlink satellites confirmed https://twitter.com/SpaceX/status/1331424769632215040
    Tue Nov 24 20:23:17 CST 2020: Falcon 9’s first stage lands on the Of Course I Still Love You droneship! https://twitter.com/SpaceX/status/1331423180431396864
    Tue Nov 24 20:14:20 CST 2020: Liftoff! https://twitter.com/SpaceX/status/1331420926450094080
    Tue Nov 24 20:02:38 CST 2020: Watch Falcon 9 launch 60 Starlink satellites ? https://www.spacex.com/launches/index.html  https://twitter.com/i/broadcasts/1ypKdgVXWgRxW
    Tue Nov 24 19:43:14 CST 2020: T-30 minutes until Falcon 9 launches its sixteenth Starlink mission. Webcast goes live ~15 minutes before liftoff https://www.spacex.com/launches/index.html
    Tue Nov 24 18:00:59 CST 2020: RT @elonmusk: Good Starship SN8 static fire! Aiming for first 15km / ~50k ft altitude flight next week. Goals are to test 3 engine ascent,…
    Mon Nov 23 15:45:38 CST 2020: Now targeting Tuesday, November 24 at 9:13 p.m. EST for Falcon 9’s launch of Starlink, when weather conditions in the recovery area should improve
    Sun Nov 22 20:45:13 CST 2020: Standing down from today’s launch of Starlink. Rocket and payload are healthy; teams will use additional time to complete data reviews and are now working toward backup opportunity on Monday, November 23 at 9:34 p.m. but keeping an eye on recovery weather
    Sat Nov 21 22:09:12 CST 2020: More Falcon 9 launch and landing photos ? https://www.flickr.com/photos/spacex https://twitter.com/SpaceX/status/1330362669837082624
    

    Where to get Auth Tokens for your app

    【讨论】:

      猜你喜欢
      • 2014-01-20
      • 1970-01-01
      • 2015-06-01
      • 2016-02-20
      • 1970-01-01
      • 2021-09-24
      • 1970-01-01
      • 2011-08-17
      • 2020-12-09
      相关资源
      最近更新 更多