简体   繁体   English

如何在rails app中设置Twitter gem?

[英]How to set up the Twitter gem in rails app?

I'm a newbie developer a little mystified/overwhelmed by the documentation. 我是一个新手开发者,有点神秘/不知所措。 I'm wondering how to implement the Twitter gem in a Rails 3.1 application. 我想知道如何在Rails 3.1应用程序中实现Twitter gem。

  1. I have the gem installed and know that I need to configure it, but where would I put this configuration information in a rails application? 我安装了gem并且知道我需要配置它,但是我将这个配置信息放在rails应用程序中的哪个位置? and would it need to be altered in some way to get it synched with the rails app? 是否需要以某种方式进行更改以使其与rails应用程序同步?

     Twitter.configure do |config| config.consumer_key = ENV['CONSUMER_KEY'] config.consumer_secret = ENV['CONSUMER_SECRET'] config.oauth_token = ENV['ACCESS_TOKEN'] config.oauth_token_secret = ENV['TOKEN_SECRET'] end 
  2. do I have to require 'twitter' anywhere? 我必须在任何地方要求'推特'吗?

  3. If I want visitors to my app to be update their status from my app, do I have to install Omniauth? 如果我希望我的应用程序的访问者从我的应用程序更新其状态,我是否必须安装Omniauth? or will this gem be sufficient? 或者这个宝石是否足够? ie if I require 'twitter' in the gem file and do bundle install, will bundler take care of everything I need? 即如果我在gem文件中需要'twitter'并进行捆绑安装,那么捆绑商会处理我需要的一切吗?

First question, are you using devise? 第一个问题,你在使用设计吗? If you are, this should be easier, as I'm building my own app with devise + omniauth + twitter. 如果你是,这应该更容易,因为我正在用devise + omniauth + twitter构建我自己的应用程序。 If you aren't, even though I'm a newbie rails developer myself, I'll try to steer you in the right path. 如果你不是,即使我自己是一个新手轨道开发人员,我会试着引导你走正确的道路。

For starters, you should have a twitter account. 对于初学者,你应该有一个推特账号。 You should then login here and create your app. 然后,您应该在此处登录并创建您的应用。 There, in the Settings tab you can choose the type of access or permissions you want to your user's twitter accounts. 在“ Settings选项卡中,您可以选择您希望用户的Twitter帐户访问或权限的类型。 In my case, I'm using Read only , that is, I'm only using Twitter for authentication. 在我的情况下,我使用Read only ,也就是说,我只使用Twitter进行身份验证。 Since you may want your users to update their statuses, you might want to take a look at the other 2 options: Read and Write and Read, Write and Access direct messages . 由于您可能希望用户更新其状态,因此您可能需要查看其他两个选项: Read and Write以及Read, Write and Access direct messages You should also define a callback URL which is where Twitter returns after a successful authentication. 您还应该定义一个回调URL,这是Twitter成功验证后返回的位置。 Then you have several cosmetic options to pretty up your oauth dialog. 然后你有几个美容选项来完善你的oauth对话框。 In the Details tab, you'll find your Consumer key and Consumer secret which you should never reveal and which you'll need in your Rails app. Details选项卡中,您将找到您永远不会泄露的Consumer keyConsumer secret ,以及您在Rails应用程序中需要的Consumer key

In your app, you should be including these gems (and running bundle install afterwards): 在您的应用程序中,您应该包含这些gem(并在之后运行bundle install ):

#Gemfile
gem 'omniauth'
gem 'omniauth-twitter'

Then, in your initializers, you should create a file called omniauth.rb (you can call it whatever you like, but it's the standard) with the following: 然后,在初始化程序中,您应该创建一个名为omniauth.rb的文件(您可以随意调用它,但它是标准的),具有以下内容:

#config/initializers/omniauth.rb
require 'omniauth-twitter'
config.omniauth :twitter, ENV['TWITTER_KEY'], ENV['TWITTER_SECRET']

In my case, since I'm using devise, I included this in my devise.rb file, but it really doesn't matter where you include this as long as it's in a file in the config/initializers directory. 在我的情况下,因为我正在使用devise.rb ,所以我将它包含在我的devise.rb文件中,但只要它在config/initializers目录中的文件中包含它就没关系。

You should definitely take a look at the following sites: 你一定要看看以下网站:

Omniauth's Github Wiki Omniauth的Github Wiki

The invaluable Railscasts by Ryan Bates Ryan Bates的宝贵Railscasts

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM