如何在 rails 应用程序中设置 Twitter gem? [英] How to set up the Twitter gem in rails app?

查看:22
本文介绍了如何在 rails 应用程序中设置 Twitter gem?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名新手开发人员,对文档有点困惑/不知所措.我想知道如何在 Rails 3.1 应用程序中实现 Twitter gem.

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.

  1. 我已经安装了 gem 并且知道我需要对其进行配置,但是我会将这些配置信息放在 rails 应用程序中的什么位置呢?是否需要以某种方式对其进行更改以使其与 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

  • 我必须在任何地方都需要twitter"吗?

  • do I have to require 'twitter' anywhere?

    如果我希望我的应用程序的访问者从我的应用程序更新他们的状态,我是否必须安装 Omniauth?还是这颗宝石就足够了?即,如果我需要在 gem 文件中使用twitter"并进行捆绑安装,捆绑器会处理我需要的一切吗?

    If I want visitors to my app to be update their status from my app, do I have to install Omniauth? or will this gem be sufficient? i.e. if I require 'twitter' in the gem file and do bundle install, will bundler take care of everything I need?

    推荐答案

    第一个问题,你在使用 devise 吗?如果你是,这应该更容易,因为我正在使用 devise + omniauth + twitter 构建我自己的应用程序.如果你不是,即使我自己是一个新手 Rails 开发人员,我也会尝试引导你走上正确的道路.

    First question, are you using devise? If you are, this should be easier, as I'm building my own app with 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.

    首先,您应该有一个推特帐户.然后,您应该在此处登录并创建您的应用.在那里,在 Settings 标签中,您可以选择您想要访问用户 Twitter 帐户的类型或权限.就我而言,我使用的是 Read only,也就是说,我只使用 Twitter 进行身份验证.由于您可能希望您的用户更新他们的状态,您可能需要查看其他 2 个选项:读取和写入读取、写入和访问直接消息.您还应该定义一个回调 URL,这是 Twitter 在成功验证后返回的位置.然后你有几个装饰选项来美化你的 oauth 对话框.在 Details 选项卡中,您将找到您的 Consumer keyConsumer secret ,您永远不应该透露它们并且您需要在 Rails 中使用它们应用.

    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. In my case, I'm using Read only, that is, I'm only using Twitter for authentication. 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. You should also define a callback URL which is where Twitter returns after a successful authentication. Then you have several cosmetic options to pretty up your oauth dialog. 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.

    在您的应用中,您应该包含这些 gem(然后运行 ​​bundle install):

    In your app, you should be including these gems (and running bundle install afterwards):

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

    然后,在您的初始值设定项中,您应该创建一个名为 omniauth.rb 的文件(您可以随意命名,但这是标准名称),内容如下:

    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:

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

    就我而言,由于我使用的是 devise,因此我将其包含在我的 devise.rb 文件中,但是只要将它包含在config/initializers 目录.

    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.

    您绝对应该查看以下网站:

    You should definitely take a look at the following sites:

    Omniauth 的 Github Wiki

    Ryan Bates 的宝贵 Railscasts

    这篇关于如何在 rails 应用程序中设置 Twitter gem?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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