2
votes

I'm very new to twitter API (using Twitter4j) to read my tweets. I have set up my app with the twitter and got the KEY,SECRET and TOKEN/TOKENSECRET

Below is the code which I have written.

   public final class ReadTweets {

   public static void main(String[] args) {

ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
        .setOAuthConsumerKey("myKEY")
        .setOAuthConsumerSecret("mySECRET")
        .setOAuthAccessToken("myTOKEN")
        .setOAuthAccessTokenSecret("myTOKENSECRET");

TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();

try {
    ResponseList<Status> a = twitter.getUserTimeline(new Paging(1,5));
    for(Status b :a){
        System.out.println(b.getText());
    }
} catch (TwitterException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
}


}

But Im getting the following error.

  api.twitter.com
  Relevant discussions can be found on the Internet at:
  http://www.google.co.jp/search?q=4be80492 or
  http://www.google.co.jp/search?q=0bf5b837
  TwitterException{exceptionCode=[4be80492-0bf5b837 d7e149ce-72c14a70], statusCode=-1,      message=null, code=-1, retryAfter=-1, rateLimitStatus=null, version=4.0.2}
  at twitter4j.HttpClientImpl.handleRequest(HttpClientImpl.java:178)
  at twitter4j.HttpClientBase.request(HttpClientBase.java:53)
  at twitter4j.HttpClientBase.get(HttpClientBase.java:71)
  at twitter4j.TwitterImpl.get(TwitterImpl.java:1556)
  at twitter4j.TwitterImpl.getUserTimeline(TwitterImpl.java:167)
  at com.test.twitter.UpdateStatus.main(UpdateStatus.java:47)
  Caused by: java.net.UnknownHostException: api.twitter.com
  at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
  at java.net.PlainSocketImpl.connect(Unknown Source)
  at java.net.SocksSocketImpl.connect(Unknown Source)
  at java.net.Socket.connect(Unknown Source)
  at sun.security.ssl.SSLSocketImpl.connect(Unknown Source)
  at sun.net.NetworkClient.doConnect(Unknown Source)
  at sun.net.www.http.HttpClient.openServer(Unknown Source)
  at sun.net.www.http.HttpClient.openServer(Unknown Source)
  at sun.net.www.protocol.https.HttpsClient.<init>(Unknown Source)
  at sun.net.www.protocol.https.HttpsClient.New(Unknown Source)
  at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(Unknown  Source)
  at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
  at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
  at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
  at java.net.HttpURLConnection.getResponseCode(Unknown Source)
  at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(Unknown Source)
  at twitter4j.HttpResponseImpl.<init>(HttpResponseImpl.java:35)
  at twitter4j.HttpClientImpl.handleRequest(HttpClientImpl.java:142)
  ... 5 more

Though I added a proxy to my Configuration Builder code and it worked.

             ConfigurationBuilder cb = new ConfigurationBuilder();
             cb.setDebugEnabled(true)
            .setOAuthConsumerKey("myKEY")
            .setOAuthConsumerSecret("MySecret")
            .setOAuthAccessToken("Token")
            .setOAuthAccessTokenSecret("TokenSecret")

            .setHttpProxyHost("ProxyServer")
            .setHttpProxyPort(Port);


    TwitterFactory tf = new TwitterFactory(cb.build());
    Twitter twitter = tf.getInstance();

My question is why do I have to use a proxy to get my code working. Is there any other way around ?

3
Hi the problem got resolved by adding the following line setOAuthAccessTokenSecret("TokenSecret").setHttpConnectionTimeout(100000);Rahuul

3 Answers

1
votes

This Error is usually caused by your vm not connected to the internet (i assume you are using virtual machine - vm). Make sure your VM is connected to the internet. try google a public website to see if it is connected. Let me give you a little background of this error. just look at your log file and notice the following line Caused by: java.net.UnknownHostException: api.twitter.com

which shows that your application does not recognize your sink and source because your vm is not connected to internet (i used to have the same problem and found out later what causes this). Also check the port number on which you are connecting your flume to the sink on your system. cheers.

0
votes

The network you are using is using a proxy as a gateway to the internet, its very hard to bypass it, you have to use proxy settings, or otherwise use another network to connect to the internet.

0
votes

Hi the problem got resolved by adding the following line setOAuthAccessTokenSecret("TokenSecret").setHttpConnectionTi‌​meout(100000)