Why doesn't this code work consistently?


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
final private static String OAUTH_ACCESS_TOKEN = "1384551-ueSEFeFP
final private static String OAUTH_ACCESS_TOKEN_SECRET = “G2XHPI*******************InJjYcGvQCM”;

public static void main(String[] args) {

Twitter twitter = null;

if (args.length < 1) {
      .println("Usage: java twitter4j.examples.user.ShowUser [screen name]");
  ConfigurationBuilder cb = new ConfigurationBuilder();
  TwitterFactory tf = new TwitterFactory(cb.build());
  twitter = tf.getInstance();
try {

  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.getScreenName() + " - "
        + user.getStatus().getText());
  } else {
    // the user is protected
    System.out.println("@" + user.getScreenName());
} catch (TwitterException te) {
  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!");