Question

I need to get the number of tweets,followers count and friends count of a person say virat kohli.I use Twitter4j jar and my code looks like this

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.User;
import twitter4j.auth.OAuthAuthorization;
import twitter4j.conf.ConfigurationBuilder;


public class HomePage {
public static void main(String[] args) throws IOException, TwitterException {
    Properties props=new Properties();
    InputStream input=new FileInputStream(args[0]);
    props.load(input);
    ConfigurationBuilder conf=new ConfigurationBuilder();
         conf.setDebugEnabled(true).setOAuthConsumerKey(props.getProperty("ConsumerKey")).setOAuthConsumerSecret(props.getProperty("ConsumerSecret")).setOAuthAccessToken(props.getProperty("AccessToken")).setOAuthAccessTokenSecret(props.getProperty("AccessTokenSecret"));
    OAuthAuthorization auth = new OAuthAuthorization(conf.build());
     Twitter twitter = new TwitterFactory().getInstance(auth);
    // User user = twitter.verifyCredentials();
    User user=twitter.showUser("@imVkohli");
    System.out.println(user.getStatusesCount());
    System.out.println(user.getFriendsCount());
    System.out.println(user.getFollowersCount());




}

}

All the keys are placed in the property file and iam passing the path of property file as runtime argument. Iam getting following exception

Exception in thread "main" 401:Authentication credentials    (https://dev.twitter.com/pages/auth) were missing or incorrect. Ensure that you have set   valid consumer key/secret, access token/secret, and the system clock is in sync.
message - Could not authenticate you
code - 32

If i try to get my timeline then iam not getting this error. Is there an error in the code?

Was it helpful?

Solution

The problem was that the line

User user=twitter.showUser("@imVkohli");

It must be Screen name and not the twitter handler. The Code must be

User user=twitter.showUser("imVkohli");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top