【问题标题】:Facebook integration with ruby on railsFacebook 与 ruby​​ on rails 的集成
【发布时间】:2012-08-21 10:26:09
【问题描述】:

我正在创建一个简单的 Ruby on Rails 应用程序。它允许用户从应用程序登录到 facebook,并在成功登录后返回应用程序主页。我遵循了一些关于 rails casts 和 http://blog.yangtheman.com/2012/02/09/facebook-connect-with-rails-omniauth-devise/ 的教程。但现在我收到 http 400 错误。我已经安装了 gemsomniauth、omniauth facebook 和 devise。请帮忙。我正在发布相同的视图、模型和控制器。我的应用程序已经包含与 twitter 的集成。

Index.html.erb(可以说是应用的首页)

<h1>Twitter tatter</h1>    
<form action="create" method="post">
  <label for="keyword">Enter_keyword</label>
  <input id="keyword" name="tweet[search]" size="30" type="text" />

  <input type="submit" value="search" />

  <%= link_to 'Login with Facebook', '/auth/facebook/' %>

  <!-- 
  <a href="/auth/facebook" class="auth_provider">
    <%= image_tag "facebook_64.png", :size => "64x64", :alt => "Login_to_Facebook "%>Facebook
  </a> 
  </br> 
  -->    
</form>

</br></br></br></br></br>


<div id="container">
  <% if (@tweets != nil && @tweets.count>0) then %>

User.rb(使用设计创建的模型)

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  has_many :authentications # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me
end

身份验证模型

class Authentication < ActiveRecord::Base
  belongs_to :user
end

身份验证控制器

def create
  auth = request.env["omniauth.auth"]
  authentication = Authentication.find_by_provider_and_uid(auth['provider'], auth['uid'])
  flash[:notice] = "Signed in successfully."
  sign_in_and_redirect(:user, authentication.user)
end

Routes.rb

NewYearTweets::Application.routes.draw do
  devise_for :users
  resources :authentications

resources :tweetscontroller

get "tweets/index"

match 'tweets/create' => 'tweets#create'

match '/auth/:facebook/callback' => 'authentications#create

我发布的是与 facebook 集成相关的代码,而不是与 twitter 集成相关的代码,因为它工作正常。成功登录facebook后,我想重定向到主页即索引页

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1 devise


    【解决方案1】:

    我确定在您的User.rb 中,您需要将:omniauthable 包含在devise 模块列表中。好像你漏掉了。

    【讨论】:

      【解决方案2】:

      这是在 ruby​​ on rails 中集成 facebook 的另一种方法

      1. 在您的 gem 文件中,添加此 gem -> gem 'omniauth-facebook' -> 这是用于 facebook 身份验证的 gem。

      2. 在你的 config/initializer 上创建一个文件 -> omniauth.rb -> 在omniauth.rb 里面放这行:

        Rails.application.config.middleware.use OmniAuth::Builder 做 提供者 :facebook, 'APPID', 'SECRET KEY', {:scope => 'publish_stream', :client_options => {:ssl => {:ca_path => '/etc/ssl/certs'}}}

        OmniAuth.config.logger = Rails.logger 结束

      步骤 3 和 4 将修复 ssl 错误。

      1. 在你的 config/initilizer 上创建一个文件 -> fix_ssl.rb -> 在 fix_ssl.rb 里面放这行:

        需要'open-uri' 需要'net/https'

        模块网络 HTTP 类 别名方法 :original_use_ssl=, :use_ssl=

        def use_ssl=(flag)
          #self.ca_file = Rails.root.join('lib/ca-bundle.crt')
          self.ca_file = Rails.root.join('lib/ca-bundle.crt').to_s
          self.verify_mode = OpenSSL::SSL::VERIFY_PEER
          self.original_use_ssl = flag
        end
        

        结束 结束

      2. 在这里http://jimneath.org/2011/10/19/ruby-ssl-certificate-verify-failed.html下载ca-bundle.crt文件并放到你的lib目录中

      3. 创建一个 facebook.js,在这个文件中放置 Facebook-SDK -> 在 facebook.js 里面放这个

        window.fbAsyncInit = function() {
          FB.init({
            appId      : "APPID", // App ID
            channelUrl : '//localhost:3001/channel.html', // Channel File for x-domain communication -> change this based on your Facebook APP site url.
            status     : true, // check login status
            cookie     : true, // enable cookies to allow the server to access the session
            xfbml      : true  // parse XFBML
        
          });
        
            $(function(){
                $("ID OF YOUR LOGIN BUTTON").click(function(){
                FB.login(function(response) {
                    if (response.authResponse) {
                        window.location = "auth/facebook";
                        return false;
                    }
                    }, {scope: 'email,read_stream,publish_stream,offline_access'});
                });
        
        
            $(function() {
                $("ID OF YOUR LOGOUT BUTTON").click(function(){
                    FB.getLoginStatus(function(response){
                        if(response.authResponse){
                            FB.logout();
                        }
                    });
                });
            }); 
         };
        

        // 异步加载 SDK (函数(d){ var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0]; if (d.getElementById(id)) {return;} js = d.createElement('脚本'); js.id = id; js.async = true; js.src = '//connect.facebook.net/en_US/all.js'; ref.parentNode.insertBefore(js, ref); }(文档));

      就是这样…… 不要忘记添加 -> 需要集成 facebook 插件 -> 需要集成 facebook 插件

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-01-07
        • 2012-03-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-06-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多