Problem with Java


i’m writing a java application to analyze a stream of tweets but when i connect to and i try to login with my account credentials nothing happens,

this is my code and the error it gives.

package javaapplication5; import; import; import; import; import; import; import; import; import; import; import; import; import twitter4j.*; import twitter4j.auth.AccessToken; import twitter4j.auth.RequestToken; public class Main {
public static void main(String[] args) throws TwitterException, MalformedURLException, IOException {
 strTwitter og=new strTwitter();


class strTwitter
public void start() throws TwitterException, MalformedURLException, IOException

      Twitter twitter = TwitterFactory.getSingleton();
twitter.setOAuthConsumer("********", "***********");
RequestToken requestToken = twitter.getOAuthRequestToken();
AccessToken accessToken = null;
BufferedReader br = new BufferedReader(new InputStreamReader(;
while (null == accessToken) {
  System.out.println("Open the following URL and grant access to your account:");
  System.out.print("Enter the PIN(if aviailable) or just hit enter.[PIN]:");
  String pin = br.readLine();
     if(pin.length() > 0){
       accessToken = twitter.getOAuthAccessToken(requestToken, pin);
       accessToken = twitter.getOAuthAccessToken();
  } catch (TwitterException te) {
    if(401 == te.getStatusCode()){
      System.out.println("Unable to get the access token.");
 Authenticator.setDefault(new CustomAuthenticator());
    URL url=new URL("");
    HttpURLConnection conn=(HttpURLConnection) url.openConnection();
    BufferedReader bf = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String line="";

class CustomAuthenticator extends Authenticator {

    protected PasswordAuthentication getPasswordAuthentication() {

        String prompt = getRequestingPrompt();
        String hostname = getRequestingHost();
       InetAddress ipaddr = getRequestingSite();
        int port = getRequestingPort();
        String username = ******";

        String password = "********";
        return new PasswordAuthentication(username, password.toCharArray());




Exception in thread “main” Server redirected too many times (20)
at javaapplication5.strTwitter.start(
at javaapplication5.Main.main(
Java Result: 1

can anyone help me?