How to get tweets of a specific user using twitter4j
Asked Answered
B

1

6

I am writing an app that should fetch tweets of a specific twitter user. So I have to colect screen name first then should fetch tweets. I tried with the below code.

package gethometimeline;
import twitter4j.*;
import java.util.*;
import twitter4j.conf.ConfigurationBuilder;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;

import java.util.List;

public class GetUserTimeLine {


    public static void main(String[] args) {

                   ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
  .setOAuthConsumerKey("")
  .setOAuthConsumerSecret("")
  .setOAuthAccessToken("")
  .setOAuthAccessTokenSecret("");
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();



        try {
            List<Status> statuses;


             Scanner input = new Scanner(System.in);
             System.out.println("Enter Twitter Screen Name: ");
             String user = input.nextLine();

            if (args.length == 1) {
                user = args[0];
                statuses = twitter.getUserTimeline(user);
            } else {
                user = twitter.verifyCredentials().getScreenName();
                statuses = twitter.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);
        }
    }

By running this code, i got the tweets of user whose consumer key and consumer secret key I enter in configuration. But I need to get the tweets of specific screen name.

Behoof answered 23/11, 2015 at 3:21 Comment(0)
P
2

sample Implementation of twitter4j for user tweets here. You need to pass the user name as command line parameter while running the program . I don't see any other issue with this code

Pirbhai answered 23/11, 2015 at 5:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.