显示来自Rails应用程序的Twitter提要 [英] Display a Twitter feed from a Rails app

查看:71
本文介绍了显示来自Rails应用程序的Twitter提要的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经能够使用户通过OmniAuth与Twitter登录(我遵循Railscast#235-6,并做了一个简单的应用程序).现在,我试图显示已登录用户的Twitter feed.谁能告诉我这是怎么做的?如何初始化Twitter?如何输入登录用户的用户名和密码?我是Rails的新手,所以如果我确切知道将代码放置在什么地方会很有帮助.谢谢

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 logged in user. Can anyone tell me how this is done? How do I initialize Twitter? How do I pass in the username and password of the logged in user? I am new to Rails so it would be helpful if I knew exactly where to put the code. Thanks

推荐答案

首先,如果Twitter feed是公开的,则不需要用户凭据即可获取.看着那(这 Twitter gem .一旦安装了gem,您所需要做的就是:

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")

在IRB中进行尝试以开始使用.很简单,对吧?

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

现在,您可能要使用API​​密钥,因为Twitter限制了匿名请求,并且在共享服务器上可能会出现问题.在初始化器中进行操作:

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

从Twitter开发人员页面获取实际值.

Get the actual values from your Twitter developer page.

最后,要真正花哨,如果您想扩大规模,可以使用从OmniAuth获得的OAuth凭据(不是用户名和密码;代表用户名)来代表用户提出请求. ).这样一来,您每秒可以发出更多请求,因为它们来自不同的用户.只需使用设置为您从OmniAuth哈希中获得的内容的consumer_keyconsumer_secret字段初始化Twitter(请参阅此处,在凭据"下查看如何从OmniAuth中获取它们.

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).

这篇关于显示来自Rails应用程序的Twitter提要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆