Question

I'm working on a personal project just to get my hands dirty with RoR. What I'm trying to do is a very simple twitter search engine using the Twitter gem. Calling the Twitter.search method with various options I can accomplish this goal easily. My problem is:

How do I show the user avatar of each tweet I retrieved from the search ?

I managed to get the json url and parse it, but I'm getting the Bad Authentication Data message:

{"errors"=>[{"message"=>"Bad Authentication data", "code"=>215}]}

I know that this might be an OAuth problem but even though I've been reading about it, I kinda get confused on how to properly use it. Is it needed to do the OAuth approval dance on a per-user basis (and eventually how) or am I able to hard-code the values from the Twitter dashboard as I did to get the results from the search (which apparently doesn't work for the avatar)?

I have my twitter.rb file like this:

Twitter.configure do |config|
  config.consumer_key = 'key'
  config.consumer_secret = 'secret'
  config.oauth_token = 'token'
  config.oauth_token_secret = 'token-secret'
end

I thought that retrieveing the user json from *https://api.twitter.com/1.1/users/show.json?user_id=...* would work but I don't know how to get the authorization for that. There are many similar questions but I still couldn't understand how to solve this problem. Any help would be much appreciated.

EDIT: In the twitter API doc related to this it says "Use the OAuth tool in this page sidebar to generate the OAuth signature for this request." but I don't understand how to use the signature to authorize the request (if that is the actual problem)

Was it helpful?

Solution

You can do what you want with this version of the twitter gem. You're using a version of the gem for < 4.8, based on your configuration options, so some of the doc has already been updated for version 5. Note that this call is deprecated in version 5, so in the future this code will have to be changed. For this version, this is how I do it:

controller:

Twitter.configure do |config|
  config.consumer_key        = consumer_key
  config.consumer_secret     = consumer_secret
  config.oauth_token        = oauth_token
  config.oauth_token_secret = oauth_token_secret
end 

@twitter_search = Twitter.search(params[:search], :count => 10, :result_type => "recent")

view (this is haml but erb or slim is similar):

%ul
  = @twitter_search.results.map do |tweet| status.profile_image_url }
    %li
      .span2
        %img{:src => status.profile_image_url}
        #{status.screen_name}
      .span4
        #{status.text}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top