Twitter集成:已经设置了消费者密钥/秘密对 [英] Twitter integration:consumer key/secret pair already set

查看:209
本文介绍了Twitter集成:已经设置了消费者密钥/秘密对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用 twitter4j lib将我的Web应用程序与Twitter集成.
我已经在Twitter网站上注册了我的应用,并获得了Consumer keyConsumer secret值.
没什么特别的,标准的OAuth步骤.

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.

代码:

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);
...

例外:

Caused by: java.lang.IllegalStateException: consumer key/secret pair already set.

我没有对keysecret,任何.properties或其他文件的更多配置.

I have no more configuration for key and secret,any .properties or other file.

注释行twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);导致异常:

java.lang.IllegalStateException: OAuth consumer key/secret combination not supplied

推荐答案

同时查看代码和文档,似乎不建议您实例化Twitter实例的方法.如果要以编程方式提供配置(而不使用属性),则似乎需要向TwitterFactory提供Configuration.

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();
...

由工厂提供的未提供配置的单例默认使用由PropertyConfiguration配置支持的Authorization实现.如果没有属性文件,则它看起来 就像它不应该实例化OAuthAuthorization身份验证,这将导致您看到异常.但是PropertyConfiguration会在整个CLASSPATH中搜索适当的属性文件,因此您可能忽略了它.您可以在获取Twitter实例后立即尝试记录密钥和机密,以查看它们设置为 的情况:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆