简体   繁体   English

缺少 Twitter4j 身份验证凭据

[英]Twitter4j authentication credentials are missing

I would like to make a tweet with Twitter4j in my Android app.我想在我的 Android 应用程序中使用 Twitter4j 发一条推文。 Here is my code:这是我的代码:

 //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", "");

    }
}

I always get this error message from Twitter4j: java.lang.IllegalStateException: Authentication credentials are missing.我总是从 Twitter4j 收到此错误消息:java.lang.IllegalStateException:身份验证凭据丢失。 See http://twitter4j.org/en/configuration.html for the detail.有关详细信息,请参阅http://twitter4j.org/en/configuration.html But as you can see I'm using builder to set my key.但是正如您所看到的,我正在使用 builder 来设置我的密钥。 Can someone help me to fix it please?有人可以帮我解决吗? thanks.谢谢。

Problem is following lines.问题是以下几行。

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

You are passing the configuration to one TwitterFactory instance and using another TwitterFactory instance to get the Twitter instance.您将配置传递给一个TwitterFactory实例并使用另一个TwitterFactory实例来获取Twitter实例。

Hence, You are getting java.lang.IllegalStateException: Authentication credentials are missing因此,您收到java.lang.IllegalStateException: Authentication credentials are missing

I suggest you to modify your code as follows:我建议你修改你的代码如下:

    //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();

And use this twitter instance.并使用这个 twitter 实例。 It will work.它会起作用。

I was having issues with the configuration on Twitter4j because I was not providing the right configuration.我在 Twitter4j 上遇到配置问题,因为我没有提供正确的配置。 So in order to fix it, I created the following function to establish my configuration to later be used in another function:所以为了修复它,我创建了以下函数来建立我的配置,以便以后在另一个函数中使用:

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;
}

To get the required info, you must have a Twitter developer account, and to get the auth info of an app previously created go to: Projects and Apps .要获取所需信息,您必须拥有 Twitter 开发者帐户,要获取之前创建的应用程序的身份验证信息,请访问:项目和应用程序

In the end, I was able to retrieve the data from a SpaceX account:最后,我能够从 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从何处获取应用程序的身份验证令牌

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

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