Having multiple Twitter instances with twitter4j library.

江枫思渺然 提交于 2019-12-01 04:45:24

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

A Twitter instance can handle only one account. To deal with multiple accounts, you need to instantiate multiple Twitter instances with TwitterFactory#getInstance(AccessToken);

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

Damn it a little bit more googling would have saved me 100 rep.

Let's be clear: there is a difference between Twitter and TwitterStream instances. Twitter instances can indeed have one consumer key+ secret and multiple OAuth tokens. That answers the question by the OP.

Now, for Twitterstream instances (that are needed to access the Twitter Streaming API), that's different. You can only have only one TwitterStream ojbect running at a given time, not several even if they have different OAuth tokens.

According to dev.twitter.com,

You'll be best served by sticking to the concept of "one account, one application, one open connection" when connecting to the stream.twitter.com endpoints. If there are other distinct applications you plan to use with stream.twitter.com, I would strive to follow the same 1:1:1 concept for it.

You may find that at times stream.twitter.com lets you get away with more open connections here or there, but that behavior shouldn't be counted on.

I give the bounty to Yusuke Yamamoto since he provided the correct answer first, and do consider upvoting this one too if it clarifies things for you! ;-)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!