带有twitter4j库的多个Twitter实例。 [英] Having multiple Twitter instances with twitter4j library.
问题描述
我需要能够使用相同的消费者密钥和秘密,但具有不同的用户级别访问令牌来制作不同的 Twitter
实例。
I need to be able to make different Twitter
instances with same consumer key and secret but different user level access tokens.
我已经使用标准的Twitter OAuth登录协议根据我的应用的消费者凭据为10个用户获取了访问令牌。现在,我想创建一个Twitter实例列表,每个实例都用一个单独的 Accesstoken
初始化。但是,我无法执行此操作。
I have already obtained the access tokens for 10 users based on consumer credentials of my app by using standard Twitter OAuth Sign In protocol. Now, I want to create a list of Twitter Instances each initialized with a separate Accesstoken
. However, I'm not able to do this.
我引起混乱的主要原因是,我不理解 Twitter4j
如何实例化Twitter实例。我不理解 TwitterFactory.getSingleton()
方法和 TwitterFactory()。getInstance()
方法之间的区别。
My leading cause of confusion is, that, I don't understand how Twitter4j
instantiates Twitter instances. I don't understand the difference between TwitterFactory.getSingleton()
method and TwitterFactory().getInstance()
method.
我也不知道AccessToken是否可以在不提供使用者密钥和秘密的情况下独立工作。还是在使用Accesstokens时需要提供消费者密钥和机密。
I also don't understand if AccessTokens can work independently without supplying consumer key and secret. Or do I need to provide Consumer key and secret while using Accesstokens.
如果有人可以详细解释我的要求的设计,将不胜感激。
If someone can explain in detail the design for my requirement it'll be highly appreciated.
推荐答案
简而言之,您可能需要这样的代码:
In short, you may want code like this:
TwitterFactory tf = new TwitterFactory();
Twitter user1 = tf.getInstance(new AccessToken("XXX","XXX"));
Twitter user2 = tf.getInstance(new AccessToken("XXX","XXX"));
Twitter实例只能处理一个帐户。
要处理多个帐户,您需要使用TwitterFactory#getInstance(AccessToken)实例化多个Twitter实例;
A Twitter instance can handle only one account. To deal with multiple accounts, you need to instantiate multiple Twitter instances with TwitterFactory#getInstance(AccessToken);
不建议使用硬编码的使用者密钥/秘密并且您可能想要在twitter4j.properties中指定它们。
http://twitter4j.org/en/configuration.html#fileconfiguration
Hard-coding consumer key/secret is not suggested and you may want to specify them in twitter4j.properties. http://twitter4j.org/en/configuration.html#fileconfiguration
这篇关于带有twitter4j库的多个Twitter实例。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!