Question

Hi I am new to twitter and back from Python to Java.

I am trying to use oauth to authenticate and grab tweets in real time using twitter4j.

So when I run the code listing below It looks ok but I am not sure if I am connecting!

Furthermore, I am not sure how to listen to the stream and extract tweets.

My code so far:

package twitterfordatalake;

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.Properties;

    import twitter4j.StallWarning;
    import twitter4j.Status;
    import twitter4j.StatusDeletionNotice;
    import twitter4j.StatusListener;
    import twitter4j.TwitterException;
    import twitter4j.TwitterStream;
    import twitter4j.TwitterStreamFactory;
    import twitter4j.conf.ConfigurationBuilder;
    import static twitterfordatalake.TwitterForDataLake.props;
    //Testing
    import twitter4j.TwitterFactory; 
    import twitter4j.Twitter; 
    import twitter4j.Query;
    import twitter4j.QueryResult;

    /**
     *
     * @author crigano
     */
    public class TwitterForDataLake 
    {
        static Properties props = new Properties();

        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) throws TwitterException, IOException
        {
              // TODO code application logic here
              System.out.println("Hellow World");

              TwitterForDataLake ts = new TwitterForDataLake();
              ts.TwitterScraper();

         }

        public void TwitterScraper() throws IOException
        {
                // read the properties file options
                ConfigurationBuilder cb = new ConfigurationBuilder();
                cb.setDebugEnabled(true)
                .setOAuthConsumerKey(
                    props.getProperty("oauth.consumerKey"))
                .setOAuthConsumerSecret(
                    props.getProperty("oauth.consumerSecret"))
                .setOAuthAccessToken(
                    props.getProperty("oauth.accessToken"))
                .setOAuthAccessTokenSecret(
                    props.getProperty("oauth.accessTokenSecret"));

               TwitterStream twitterStream = new TwitterStreamFactory(cb.build())
                    .getInstance(); 

           }

    }

I used the following initialization twitter4j.properties file:
debug=true
oauth.consumerKey=AAAA
oauth.consumerSecret=BBBB
oauth.accessToken=CCCC
oauth.accessTokenSecret=DDDD

This is based on what I got from twitter:
AAAA
API secret BBBB
Access level Read, write, and direct messages (modify app permissions)
Owner me
Owner ID 66666 

Your access token
This access token can be used to make API requests on your own account's behalf. Do not share your access token secret with anyone.
Access token CCCC
Access token secret DDDD
Access level Read, write, and direct messages
Owner me
Owner ID 6666

I would very much appreciate some suggestions on debugging the oath.

I would very much appreciate any examples code for a listener for English tweets for an hour period.

Thanks Very much,

Chris

Was it helpful?

Solution

I pasted below my running code in java. You can use it for the same purpose:

//First we need to have a twitter object with a valid auth token.

public static ConfigurationBuilder confBuilder = null;
public static Configuration config = null;
public static Twitter twitterObj = null;

confBuilder = new twitter4j.conf.ConfigurationBuilder();
confBuilder.setDebugEnabled(true);

confBuilder.setOAuthConsumerKey("consumerKey");
confBuilder.setOAuthConsumerSecret("consumerSecret");
confBuilder.setOAuthAccessToken("accessToken");
confBuilder.setOAuthAccessTokenSecret("accessTokenSecret");

config = confBuilder.build();

twitter4j.TwitterFactory tf = new twitter4j.TwitterFactory(config);
twitterObj = tf.getInstance(); //Here, finally we got the twitter object.

//Later, we need to use streaming function which is also like this:

TwitterStream twitterStream = new TwitterStreamFactory(config).getInstance();

        StatusListener statusListener = new StatusListener() {
            private int count = 0;
            private long originalTweetId = 0;

            @Override
            public void onStatus(Status status) {

            // status object here is the tweet object that caught by the stream. You    can do whatever you want with it.

            }

            @Override
            public void onDeletionNotice(StatusDeletionNotice sdn) {
                 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            }

            @Override
            public void onTrackLimitationNotice(int i) {
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            }

            @Override
            public void onScrubGeo(long l, long l1) {
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            }

            @Override
            public void onStallWarning(StallWarning sw) {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public void onException(Exception ex) {
                logWriter.WriteErrorLog(ex, "onException()");
            }
        };

        FilterQuery fq = new FilterQuery();

        String keywords[] = {keyword};

        fq.track(keywords);

        twitterStream.addListener(statusListener);
        twitterStream.filter(fq);

// I hope this is what you are looking for. Good luck!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top