i’m writing a java application to analyze a stream of tweets but when i connect to https://stream.twitter.com/1.1/statuses/filter.json?track=obama and i try to login with my account credentials nothing happens,
this is my code and the error it gives.
package javaapplication5;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.net.URLConnection;
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();
og.start();
}
}
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(System.in));
while (null == accessToken) {
System.out.println("Open the following URL and grant access to your account:");
System.out.println(requestToken.getAuthorizationURL());
System.out.print("Enter the PIN(if aviailable) or just hit enter.[PIN]:");
String pin = br.readLine();
try{
if(pin.length() > 0){
accessToken = twitter.getOAuthAccessToken(requestToken, pin);
}else{
accessToken = twitter.getOAuthAccessToken();
}
} catch (TwitterException te) {
if(401 == te.getStatusCode()){
System.out.println("Unable to get the access token.");
}else{
te.printStackTrace();
}
}
Authenticator.setDefault(new CustomAuthenticator());
URL url=new URL("https://stream.twitter.com/1.1/statuses/filter.json?track=obama");
HttpURLConnection conn=(HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
BufferedReader bf = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line="";
while((line=bf.readLine())!=null)
{
System.out.println(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” java.net.ProtocolException: Server redirected too many times (20)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1315)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:234)
at javaapplication5.strTwitter.start(Main.java:65)
at javaapplication5.Main.main(Main.java:22)
Java Result: 1
can anyone help me?