3

簡単な Ruby on Rails アプリケーションを作成しています。ユーザーはアプリケーションから facebook にログインでき、ログインに成功するとアプリケーションのホームページに戻ります。Rails キャストとhttp://blog.yangtheman.com/2012/02/09/facebook-connect-with-rails-omniauth-devise/に関するいくつかのチュートリアルに従いました。しかし、今はhttp 400 エラーが発生しています。gem omniauth 、 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 (deviseで作成したモデル)

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

正常に動作しているため、Twitter 統合に関連するコードではなく、Facebook 統合に関連するコードを投稿しています。Facebookへのサインインに成功した後、インデックスページであるホームページにリダイレクトしたい

4

2 に答える 2

0

モジュールのリストUser.rbに含める必要があると確信しています。あなたはそれを残したようです。:omniauthabledevise

于 2012-08-23T02:54:47.137 に答える
0

FacebookをRuby on Railsに統合する方法の別の方法は次のとおりです

  1. gem ファイルに、この gem を追加します -> gem 'omniauth-facebook'-> これは facebook 認証用の gem です。

  2. config/initializer にファイルを作成します -> omniauth.rb -> omniauth.rb の中に次の行を入れます:

    Rails.application.config.middleware.use OmniAuth::Builder do provider :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' が必要

    module ネットクラス HTTP alias_method :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 を非同期的にロードします (function(d){ var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0]; if (d.getElementById(id)) {return;} js = d.createElement('script'); js.id = id; js.async = true; js.src = '//connect.facebook.net/en_US/all.js'; ref.parentNode.insertBefore(js , ref); }(ドキュメント));

それだけです....追加することを忘れないでください-> facebookプラグインの統合に必要です-> facebookプラグインの統合に必要です

于 2013-03-06T05:38:43.350 に答える