Twitter 集成:已设置消费者密钥/秘密对 [英] Twitter integration:consumer key/secret pair already set
问题描述
尝试使用 twitter4j 库将我的 web 应用程序与 Twitter 集成.
我已经在 twitter 网站上注册了我的应用程序并获得了 Consumer key
和 Consumer secret
值.
没什么特别的,标准的OAuth
步骤.
代码:
公共类 TwitterService {私人最终字符串 CONSUMER_KEY = "xxx";私人最终字符串 CONSUMER_SECRET = "yyy";公共字符串收藏(){推特推特 = TwitterFactory.getSingleton();twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);...
例外:
Caused by: java.lang.IllegalStateException: consumer key/secret pair already set.
我没有更多的key
和secret
配置,任何.properties
或其他文件.
注释行 twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
导致异常:
java.lang.IllegalStateException:未提供 OAuth 使用者密钥/秘密组合
从代码和文档来看,您的实例化 Twitter
实例的方法似乎是不推荐的.如果你想以编程方式提供配置(而不是使用属性),看起来你需要向 TwitterFactory
提供一个 Configuration
.
未提供配置的工厂提供的单例默认使用由 PropertyConfiguration
配置支持的 Authorization
实现.如果没有属性文件,看起来不应该实例化 OAuthAuthorization
身份验证,这会导致您看到的异常.但是 PropertyConfiguration
确实会在整个 CLASSPATH
中搜索合适的属性文件,所以您可能忽略了一个.您可以尝试在获取 Twitter
实例后立即记录密钥和秘密,以查看它们是设置的内容:
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 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());
这篇关于Twitter 集成:已设置消费者密钥/秘密对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!