下面是我的代码。我的问题是如何获取另一个帐户的用户时间线? (比如说像att这样的公共(public)账户)。我有我的消费者 key 、 secret 、访问 token 、访问 secret ,这些都在我的 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);
}
}
}
最佳答案
我怀疑如果您恢复了以下更改:
statuses = twitter1.getUserTimeline("ATT");
至:
statuses = twitter1.getUserTimeline(user);
并使用
GetUserTimeline
作为命令行参数调用 ATT
它应该可以工作。看起来 twitter4j 示例带有 utility scripts 来执行它们,因此您似乎应该能够运行:
$ getUserTimeline.sh ATT
关于java - 如何使用 twitter4j 获取用户时间线,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20503458/