如何使用twitter4j获取用户时间线

时间:2013-12-10 19:25:44

标签: java twitter4j

以下是我的代码。我的问题是如何获得另一个帐户的用户时间表? (比如像att这样的公共账户)。我有我的消费者密钥,秘密,访问令牌,访问密码,这些都是在我的Twitter帐户下注册的。请帮我。非常感谢你。

import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.conf.ConfigurationBuilder;

import java.util.List;

/**
 * @author Yusuke Yamamoto - yusuke at mac.com
 * @since Twitter4J 2.1.7
 */
public class GetUserTimeline {
    /**
     * Usage: java twitter4j.examples.timeline.GetUserTimeline
     *
     * @param args String[]
     */
    public static void main(String[] args) {
        // gets Twitter instance with default credentials
        Twitter twitter = new TwitterFactory().getInstance();
        try {
            ConfigurationBuilder cb = new ConfigurationBuilder();
            cb.setDebugEnabled(true)
              .setOAuthConsumerKey("***")
              .setOAuthConsumerSecret("***")
              .setOAuthAccessToken("***")
              .setOAuthAccessTokenSecret("***");
            TwitterFactory tf = new TwitterFactory(cb.build());
            Twitter twitter1 = tf.getInstance();
            List<Status> statuses;
            String user;
            if (args.length == 1) {
                user = args[0];
                statuses = twitter1.getUserTimeline("ATT");
            } else {
                user = twitter1.verifyCredentials().getScreenName();
                statuses = twitter1.getUserTimeline();
            }
            System.out.println("Showing @" + user + "'s user timeline.");
            for (Status status : statuses) {
                System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText());
            }
        } catch (TwitterException te) {
            te.printStackTrace();
            System.out.println("Failed to get timeline: " + te.getMessage());
            System.exit(-1);
        }
    }
}

1 个答案:

答案 0 :(得分:4)

我怀疑如果你恢复你的改变:

statuses = twitter1.getUserTimeline("ATT");

为:

statuses = twitter1.getUserTimeline(user);

并使用GetUserTimeline作为命令行参数调用ATT它应该有效。

看起来twitter4j示例附带utility scripts来执行它们,所以看起来你应该能够运行:

$ getUserTimeline.sh ATT