Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8429421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:19:59+00:00 2026-06-10T05:19:59+00:00

I’m trying to use the twitter4j library to get a random sample from the

  • 0

I’m trying to use the twitter4j library to get a random sample from the twitter stream and print the username and text of the tweet.

I’m not sure I understand the correct terminology as far as OAuth goes. Is this something I need to get for my account or only if accessing other private accounts?

Here’s my code:

package tweet1;

import java.io.IOException;

import twitter4j.Status;
import twitter4j.StatusDeletionNotice;
import twitter4j.StatusListener;
import twitter4j.TwitterException;
import twitter4j.TwitterStream;
import twitter4j.TwitterStreamFactory;
import twitter4j.auth.BasicAuthorization;

public class Twit4jEx1 {

    /**
     * @param args
     */
        // TODO Auto-generated method stub
        public static void main(String[] args) throws TwitterException, IOException{
//          BasicAuthorization ba1 = new BasicAuthorization("userName", "pword");
            ConfigurationBuilder cb = new ConfigurationBuilder();

            StatusListener listener = new StatusListener(){
                public void onStatus(Status status) {
                    System.out.println(status.getUser().getName() + " : " + status.getText());
                }
                public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {}
                public void onTrackLimitationNotice(int numberOfLimitedStatuses) {}
                public void onException(Exception ex) {
                    ex.printStackTrace();
                }
                @Override
                public void onScrubGeo(long arg0, long arg1) {
                    // TODO Auto-generated method stub

                }
            };
            TwitterStream twitterStream = new TwitterStreamFactory().getInstance();
            // sample() method internally creates a thread which manipulates TwitterStream and calls these adequate listener methods continuously.
            twitterStream.sample();
        }   
}

ConfigurationBuilder class

package tweet1;

import twitter4j.Twitter;
import twitter4j.TwitterFactory;
//import twitter4j.conf.ConfigurationBuilder;

public class ConfigurationBuilder {
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true)
      .setOAuthConsumerKey("***")
      .setOAuthConsumerSecret("***")
      .setOAuthAccessToken("***")
      .setOAuthAccessTokenSecret("***");
    TwitterFactory tf = new TwitterFactory(cb.build());
    Twitter twitter = tf.getInstance();
}

When I run Twit4jEx1 I get the following:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    Syntax error on token(s), misplaced construct(s)
    Syntax error on token "setDebugEnabled", = expected after this token

    at tweet1.ConfigurationBuilder.<init>(ConfigurationBuilder.java:9)
    at tweet1.Twit4jEx1.main(Twit4jEx1.java:21)

Any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T05:20:01+00:00Added an answer on June 10, 2026 at 5:20 am

    You class ConfigurationBuilder is trying to run code in an initialization block instead of in a constructor or method.

    I think you meant to have something like:

    public class MyConfigurationBuilder {
        private ConfigurationBuilder cb; 
        private Twitter twitter;
    
        public MyConfigurationBuilder() {
          cb = new ConfigurationBuilder();
          cb.setDebugEnabled(true)
            .setOAuthConsumerKey("***")
            .setOAuthConsumerSecret("***")
            .setOAuthAccessToken("***")
            .setOAuthAccessTokenSecret("***");
          TwitterFactory tf = new TwitterFactory(cb.build());
          twitter = tf.getInstance();
        }
    }
    

    And then you can add appropriate getters or something. Make sure you use MyConfigurationBuilder in your main funciton.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have a bunch of posts stored in text files formatted in yaml/textile (from
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.