Twitter integration:consumer key/secret pair already set

前端 未结 2 1721
旧巷少年郎
旧巷少年郎 2020-12-03 06:03

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

相关标签:
2条回答
  • 2020-12-03 06:17

    My hunch is that you're setting the private final variables and then twitter.setOAuthConsumer() is trying to do the same. You should only need one or the other. Have you tried commenting out the twitter.setOAuthConsumer() line?

    The Docs explain the 'preferred' way to do set these.

    0 讨论(0)
  • 2020-12-03 06:37

    Looking at both the code and documentation, it looks like your method of instantiating a Twitter instance is not recommended. If you want to supply configuration programmatically (and not use properties), it looks like you need to supply a Configuration to the TwitterFactory.

    ...
    ConfigurationBuilder builder = new ConfigurationBuilder();
    builder.setOAuthConsumerKey(CONSUMER_KEY);
    builder.setOAuthConsumerSecret(CONSUMER_SECRET);
    Configuration configuration = builder.build();
    TwitterFactory factory = new TwitterFactory(configuration);
    Twitter twitter = factory.getInstance();
    ...
    

    The singleton provided by a factory that hasn't been supplied with a configuration defaults to using an Authorization implementation backed by a PropertyConfiguration configuration. If there is no properties file, it looks like it shouldn't instantiate an OAuthAuthorization auth, which is what would cause the exception you're seeing. But PropertyConfiguration does search the entire CLASSPATH for an appropriate properties file, so maybe you overlooked one. You could try logging the key and secret right after getting the Twitter instance to see what they are set to:

    System.out.println("key:" + twitter.getConfiguration().getOAuthConsumerKey());
    System.out.println("secret: " + twitter.getConfiguration().getOAuthConsumerSecret());
    
    0 讨论(0)
提交回复
热议问题