Trying to integrate my webapp with Twitter using twitter4j lib.
I have registered my app on twitter site and got Consumer key and Consumer secret values.
Nothing special,standard OAuth step.
code:
public class TwitterService {
private final String CONSUMER_KEY = "xxx";
private final String CONSUMER_SECRET = "yyy";
public String fav() {
Twitter twitter = TwitterFactory.getSingleton();
twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
...
exception:
Caused by: java.lang.IllegalStateException: consumer key/secret pair already set.
I have no more configuration for key and secret,any .properties or other file.
EDIT:
commenting line twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET); causes exception:
java.lang.IllegalStateException: OAuth consumer key/secret combination not supplied
Looking at both the code and documentation, it looks like your method of instantiating a
Twitterinstance is not recommended. If you want to supply configuration programmatically (and not use properties), it looks like you need to supply aConfigurationto theTwitterFactory.The singleton provided by a factory that hasn’t been supplied with a configuration defaults to using an
Authorizationimplementation backed by aPropertyConfigurationconfiguration. If there is no properties file, it looks like it shouldn’t instantiate anOAuthAuthorizationauth, which is what would cause the exception you’re seeing. ButPropertyConfigurationdoes search the entireCLASSPATHfor an appropriate properties file, so maybe you overlooked one. You could try logging the key and secret right after getting theTwitterinstance to see what they are set to:System.out.println("key:" + twitter.getConfiguration().getOAuthConsumerKey()); System.out.println("secret: " + twitter.getConfiguration().getOAuthConsumerSecret());