Display a Twitter feed from a Rails app

后端 未结 2 524
滥情空心
滥情空心 2021-02-04 19:29

I have been able to have a user sign in with Twitter via OmniAuth (I followed Railscast #235-6 and made a simple application). Now I am trying to display the Twitter feed of the

相关标签:
2条回答
  • 2021-02-04 19:54

    First, you don't need user credentials to get a Twitter feed if it's public. Look at the Twitter gem. Once you install the gem, all you need to do is:

    require 'twitter'
    Twitter.user_timeline("icambron")
    

    Try it out in IRB to get started. Pretty easy, right?

    Now, you probably want to use your API key because Twitter limits anonymous requests, and it can be problematic from a shared server. Do that in an initializer:

    Twitter.configure do |config|
      config.consumer_key = YOUR_CONSUMER_KEY
      config.consumer_secret = YOUR_CONSUMER_SECRET
      config.oauth_token = YOUR_OAUTH_TOKEN
      config.oauth_token_secret = YOUR_OAUTH_TOKEN_SECRET
    end
    

    Get the actual values from your Twitter developer page.

    Finally, to get really fancy, if you want to scale up, you can make the request on behalf of the user, using the OAuth credentials that you got from OmniAuth (NOT their username and password; you don't have those). That will allow you to make a lot more requests per second, because they're coming from different users. Just initialize Twitter with the consumer_key and consumer_secret fields set to the stuff you got from the OmniAuth hash (see here, look under "credentials" to see how to get them from OmniAuth).

    0 讨论(0)
  • 2021-02-04 20:04

    class Tweet

        BASE_URL = "http://api.twitter.com/1.1/statuses/user_timeline.json"
        SCREEN_NAME = "OMGFacts"
        MAX_TWEETS = 10000
    
        CONSUMER_KEY = "PMiAyrY5cASMnmbd1tg"
        CONSUMER_SECRET = "0TYRYg0hrWBsr1YZrEJvS5txfA9O9aWhkEqcRaVtoA"
    
        class << self
          def base_url
            BASE_URL
          end
    
          def screen_name
            SCREEN_NAME
          end
    
          def url(count = MAX_TWEETS)
            params = {:screen_name => screen_name, :count => count}
            [base_url, params.to_param].join('?')
          end
    
          def prepare_access_token(oauth_token, oauth_token_secret)
            consumer = OAuth::Consumer.new(CONSUMER_KEY, CONSUMER_SECRET,
              { :site => "http://api.twitter.com",
                :scheme => :header,
              })
            # now create the access token object from passed values
            token_hash = { :oauth_token => oauth_token,
                           :oauth_token_secret => oauth_token_secret,
                           :open_timeout => 500000000
                         }
            access_token = OAuth::AccessToken.from_hash(consumer, token_hash )
            return access_token
          end
    
          def get(count = MAX_TWEETS)
            count = Preference.get(:2000).to_i
            access_token = prepare_access_token("178394859-cJlRaiQvqVusPAPjqC2Nn7r3Uc7wWsGua7sGHzs","3T8LCZTYXzuPLGzmWX1yRnKs1JFpfJLKemoo59Piyl8")
            response = JSON.parse access_token.request(:get, url).body
    
            response[0...count]
          end
    
        end
    
      end
    
    0 讨论(0)
提交回复
热议问题