Display a Twitter feed from a Rails app

后端 未结 2 523
滥情空心
滥情空心 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).

提交回复
热议问题