0

In my rails controller, I have an array called "usernames" which just has 10 twitter handles without the "@" symbol. I am storing the latest tweet of these users in an array called "tweets" with the following assignment:

tweets = usernames.map{|username| $client_local.user_timeline(username,count: 1)[0].text}

where $client_local is:

$client_local = Twitter::REST::Client.new do |config|
    config.consumer_key = 'xxxx'
    config.consumer_secret = 'xxxx'
    config.access_token = 'xxxx'
    config.access_token_secret = 'xxxx'
end

I used the ruby benchmark module and found that the "tweets" assignment is taking 2.5 seconds to run. I am guessing it is taking so long because the user_timeline function is being called 10 times, thus making 10 separate connections.

Looking for a solution, I went to sferik's github for configuration options, and it appears that the "bearer tokan" is what I need according to this explanation on the page:

client.user("sferik")

Note: The first time this method is called, it will make two API requests. First, it will fetch an access token to perform the request. Then, it will fetch the requested user. The access token will be cached for subsequent requests made with the same client but you may wish to manually fetch the access token once and use it when initializing clients to avoid making two requests every time.

client.bearer_token

This token never expires and will not change unless it is invalidated. Once you've obtained a bearer token, you can use it to initialize clients to avoid making an extra request.

  1. I haven't been able to find my app's bearer token or figure out how to obtain one
  2. I'm not 100% if this is the solution to my problem, or if there is something else I should be looking for.

Ideas?

Searching online, I see people suggesting to make a list of these users on my Twitter account and then making a request to "lists/statuses". This is not an option because the 10 usernames in the "usernames" array are different each time the array is set.

Community
  • 1
  • 1
Emil
  • 1,131
  • 13
  • 23

1 Answers1

0
  1. Outside of the gem, you can request a bearer token from POST /oauth2/token which is described at https://dev.twitter.com/oauth/reference/post/oauth2/token
Mark Silverberg
  • 1,249
  • 2
  • 8
  • 21
  • I read that, but I don't know what I am supposed to do with https://api.twitter.com/oauth2/token – Emil Sep 26 '14 at 21:52
  • have you tried running `$client_local.bearer_token` to see if it returns the bearer token which you can then use to create a new client? – Mark Silverberg Sep 26 '14 at 21:54
  • 1
    Just tried it. It doesn't exist. According to [sferik](https://github.com/sferik/twitter/blob/master/examples/Configuration.md), I'm supposed to set config.bearer_token = "YOUR_BEARER_TOKEN" when setting "$client_local". I don't even know if this is what I'm supposed to do to resolve the performance issue. – Emil Sep 26 '14 at 22:01