Multiple Twitter instances with twitter4j library.

I need to be able to create different instances of Twitter with the same user key and secret, but different user level access tokens .

I already received access tokens for 10 users based on the user credentials of my application using the standard OAuth Sign In protocol. Now I want to create a list of Twitter instances, each of which is initialized by a separate Accesstoken . However, I cannot do this.

My main reason for the confusion is because I don’t understand how Twitter4j creates instances of Twitter. I do not understand the difference between TwitterFactory.getSingleton() and TwitterFactory().getInstance() .

I also do not understand if AccessTokens can work independently without supplying a key and consumer secret. Or do I need to provide a consumer key and secret when using Accesstokens.

If someone can explain in detail the design of my requirement, he will be highly appreciated.

+6
source share
2 answers

In short, you might need the following code:

 TwitterFactory tf = new TwitterFactory(); Twitter user1 = tf.getInstance(new AccessToken("XXX","XXX")); Twitter user2 = tf.getInstance(new AccessToken("XXX","XXX")); 

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

A hardcoding key / secret code is not offered, and you can specify them in twitter4j.properties. http://twitter4j.org/en/configuration.html#fileconfiguration

+9
source

Damn it, a little more search engine would save me 100 reputation.

Clearly, there is a difference between instances of Twitter and TwitterStream . Twitter instances can indeed have one consumer key + secret and multiple OAuth tokens. This answers the OP question.

Now for the TwitterStream instances (which are required to access the Twitter streaming API) this is different. You can only have one twitter street that is currently running , and not several, even if they have different OAuth tokens.

According to dev.twitter.com ,

You will be best offended by the concept of “one account, one application, one open connection” when connecting to stream.twitter.com Endpoints. If there are other different applications you plan to use with stream.twitter.com, I will strive to follow the same 1: 1: 1 for it.

You may find that at times stream.twitter.com allows you to leave with more open connections here or there, but this behavior should not be calculated.

I give generosity to Yusuke Yamamoto because he first gave the right answer and thought about returning it too if he clarifies everything to you !; -)

+4
source

Source: https://habr.com/ru/post/947416/


All Articles