Why doesn't this code work consistently?


#1

This is a Twitter4J program, but I suspect that the problem involves my poor understanding of OAuth. The program works once in a while (usually it doesn’t) and I have no idea why. Need help before it drives me crazy! Thanks.

package twitter4j.examples.user;

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

public final class ShowUser {
final private static String OAUTH_CONSUMER_KEY= “0m1od0dg";
final private static String OAUTH_CONSUMER_SECRET = "Tudh37WsvM
SvS1LnnjA";
final private static String OAUTH_ACCESS_TOKEN = "1384551-ueSEFeFP
*******************XFByGRQ8Wr4”;
final private static String OAUTH_ACCESS_TOKEN_SECRET = “G2XHPI*******************InJjYcGvQCM”;

public static void main(String[] args) {

Twitter twitter = null;

if (args.length < 1) {
  System.out
      .println("Usage: java twitter4j.examples.user.ShowUser [screen name]");
  System.exit(-1);
}
  ConfigurationBuilder cb = new ConfigurationBuilder();
  cb.setDebugEnabled(true)
      .setOAuthConsumerKey(OAUTH_CONSUMER_KEY)
      .setOAuthConsumerSecret(OAUTH_CONSUMER_SECRET)
      .setOAuthAccessToken(
          OAUTH_ACCESS_TOKEN)
      .setOAuthAccessTokenSecret(OAUTH_ACCESS_TOKEN_SECRET);
  TwitterFactory tf = new TwitterFactory(cb.build());
  twitter = tf.getInstance();
try {

  System.out.println(args[0]);
  User user = twitter.showUser(args[0]);
  if (user == null)
    System.out.println("User is null");
  if (user.getStatus() != null) {
    System.out.println("@" + user.getScreenName());
    System.out.println(user.getStatus().getText());
    System.out.println("@" + user.getScreenName() + " - "
        + user.getStatus().getText());
  } else {
    // the user is protected
    System.out.println("@" + user.getScreenName());
  }
} catch (TwitterException te) {
  te.printStackTrace();
  System.out.println("Failed to delete status: " + te.getMessage());
}
try {
  twitter.updateStatus("Third successful tweet in a row (from the desktop app)!");
} catch (TwitterException e) {
  System.err.println("Error occurred while updating the status!");
}

}
}