简体   繁体   English

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

[英]how to get user timeline using twitter4j

Below is my code. 以下是我的代码。 my question is how to get the user timeline of another account? 我的问题是如何获得另一个帐户的用户时间表? (say a public account like att). (比如像att这样的公共账户)。 I have my consumer key, secret, access token, access secret, which are registered under my twitter account. 我有我的消费者密钥,秘密,访问令牌,访问密码,这些都是在我的Twitter帐户下注册的。 Please help me. 请帮我。 Thank you very much. 非常感谢你。

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

I suspect that if you reverted your change of: 我怀疑如果你恢复你的改变:

statuses = twitter1.getUserTimeline("ATT");

to: 至:

statuses = twitter1.getUserTimeline(user);

and invoked GetUserTimeline with ATT as a command line argument it should work. 并使用ATT作为命令行参数调用GetUserTimeline它应该工作。

It looks like the twitter4j examples come with utility scripts to execute them, so it seems you should be able to just run: 看起来twitter4j示例带有实用程序脚本来执行它们,所以看起来你应该能够运行:

$ getUserTimeline.sh ATT

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

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