So I've been trying to play around with Twitter4j and I got everything up till the point where I could compile everything. But there was an error that said "java.lang.IllegalStateException: consumer key/secret pair already set."
I read in a few places that I needed the ConfigureBuilder class below. However, whenever I try to compile it the compiler says it cannot find ConfigureBuilder. I have redownloaded the Twitter4j files 3 times already.
The IDE I use is BlueJ if that might help.
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import twitter4j.*;
import twitter4j.auth.AccessToken;
import java.io.IOException;
import java.net.URL;
import java.util.Arrays;
public class TwitterTyper4j {
static String consumerKeyStr = "";
static String consumerSecretStr = "";
static String accessTokenStr = "";
static String accessTokenSecretStr = "";
public static void main(String[] args) {
try {
//twitter confic
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.setOAuthConsumerKey(CONSUMER_KEY);
builder.setOAuthConsumerSecret(CONSUMER_SECRET);
Configuration configuration = builder.build();
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
twitter.setOAuthConsumer(consumerKeyStr, consumerSecretStr);
AccessToken accessToken = new AccessToken(accessTokenStr,
accessTokenSecretStr);
twitter.setOAuthAccessToken(accessToken);
twitter.updateStatus("Hello Twitter");
System.out.println("Successfully updated the status in Twitter.");
} catch (TwitterException te) {
te.printStackTrace();
}
}
}
Ensure you have a recent version twitter-core-x.y.z.jar
on the classpath – I'm sure you do already, but it's worth a check.
Then you need to import the ConfigurationBuilder
, e.g.:
import twitter4j.conf.ConfigurationBuilder;
That should, hopefully, resolve that compilation error. Although there are other problems in the code which you will need to fix – where is cb
declared for example?
EDIT:
This is a bit speculative but assuming there's a typo and that cb
and configuration
could be the same...
You have already given the consumer secret and key to the builder which was then used to construct the Twitter
instance.
Then on the Twitter
instance, which is already configured with the key and secret, you set it again:
twitter.setOAuthConsumer(consumerKeyStr, consumerSecretStr);
Note that the Javadocs for that method state:
@throws
IllegalStateException
when OAuth consumer has already been set, or the instance is using basic authorization