0

Desired Behavior: Get Tweets of users from their userID's listed in a TXT file

Work/Research done beforehand: Note: I have attempted looking up this error message, and any associated with the classes used in my code before posting this.

Note: Additionally I have MANUALLY checked the users by going to twitter and looking up their username, and they DO come up. For example, this is one of the users, and they do come up: https://twitter.com/rendrarh I have done this with MANY of the users in the list.

Note: This works when just manually entering a user (using this Get tweets of a public twitter profile ) , but not when pulling it from the file

I am using Twitter4j to get tweets of users and getting the following error:

TwitterException{exceptionCode=[7defbde2-0dc3a547], statusCode=404, message=Sorry, that page does not exist., code=34, retryAfter=-1, rateLimitStatus=RateLimitStatusJSONImpl{remaining=899, limit=900, resetTimeInSeconds=1510626518, secondsUntilReset=899}, version=4.0.4}
at twitter4j.HttpClientImpl.handleRequest(HttpClientImpl.java:164)
at twitter4j.HttpClientBase.request(HttpClientBase.java:57)
at twitter4j.HttpClientBase.get(HttpClientBase.java:75)
at twitter4j.TwitterImpl.get(TwitterImpl.java:1786)
at twitter4j.TwitterImpl.getUserTimeline(TwitterImpl.java:131)
at twitter4j.TwitterImpl.getUserTimeline(TwitterImpl.java:152)
at UserTweets.main(UserTweets.java:42)

Line 42 is : statuses.addAll(twitter.getUserTimeline(user));

Here is my code:

import java.io.File;
import java.util.*;
    import twitter4j.*;
    import twitter4j.conf.*;

public class UserTweets {       

    public static void main(String[] a) {

        ConfigurationBuilder cb = new ConfigurationBuilder();
        cb.setDebugEnabled(true)
        .setOAuthConsumerKey("key")
        .setOAuthConsumerSecret("key")
        .setOAuthAccessToken("key")
        .setOAuthAccessTokenSecret("key");

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

        String user;

        List statuses = new ArrayList();
        int userCount = 1;

        String fileName = "C:-----/spam-twitter-accounts.txt";
        File file = new File(fileName);
        Scanner input = new Scanner(file);
        List<String> list = new ArrayList<String>();



        while (userCount < 20 && input.hasNextLine()) {
          try {

            user =  list.add(input.nextLine());

            statuses.addAll(twitter.getUserTimeline(user));

            System.out.println(statuses.toString());
            if (statuses.size() == 20)
              break;
          }
          catch(TwitterException e) {

            e.printStackTrace();
          }
        }
    }

}
j doe
  • 73
  • 3
  • 11
  • Which version of Twitter4J are you using? Also, can you echo out the `user` line to see what you are passing to `getUserTimeline()`? – Terence Eden Nov 14 '17 at 11:00
  • Do you mean like a System.out.println? I just added it and I get each user id, for example: user: 276458778 . It looks like I have 4.0.4 – j doe Nov 14 '17 at 16:00
  • 1
    Twitter4j Doc (http://twitter4j.org/javadoc/twitter4j/api/TimelinesResources.html) says I can use a Long or a String... BUT Long is userId and String is screenname.... maybe I am accidentally converting them to String but its looking for screenname using a userId..which could cause the error? Yep that seems to have gotten rid of the error thanks for helping me think in a better way! – j doe Nov 14 '17 at 16:06
  • That indeed was a reason for me too. I was storing userId as a string and calling the method which was supposed to take the string as a parameter – ahsan_cse2004 Jan 30 '20 at 13:27

1 Answers1

0

Needed to change it to Long since the txt file had numbers:

while (userCount < 20 && input.hasNextLong()) {
      try {

        user =  input.nextLong();
j doe
  • 73
  • 3
  • 11