I am creating a simple Ruby on Rails application. It allows users to login to facebook from the application and on successful login it returns to the application homepage. I followed some of the tutorials on rails casts and http://blog.yangtheman.com/2012/02/09/facebook-connect-with-rails-omniauth-devise/ . But now i am getting http 400 error . I have installed the gems omniauth , omniauth facebook and devise. Kindly help. I m posting the views, models and controllers for the same. My application already contains integration with twitter.
Index.html.erb (This you can say is the home page of the application)
<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 (Model created using 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
Authentications model
class Authentication < ActiveRecord::Base
belongs_to :user
end
Authentications-controller
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
I am posting code related to facebook integration, not the code related to twitter integration as it is working fine. After successful sign in to facebook, i want to redirect to the homepage that is the index page
I'm sure in your User.rb
you'll need to include :omniauthable
in the list of devise
modules. Seems like you left that out.
Here's the other way on how to integrate facebook in ruby on rails
on your gem file, add this gem -> gem 'omniauth-facebook'
-> this is the gem for facebook authentication.
create a file on your config/initializer -> omniauth.rb -> inside of omniauth.rb put this line:
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 end
create a file on your config/initilizer -> fix_ssl.rb -> inside of fix_ssl.rb put this line:
require 'open-uri' require 'net/https'
module Net class 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
end end
download the ca-bundle.crt file here http://jimneath.org/2011/10/19/ruby-ssl-certificate-verify-failed.html and put it on your lib directory
Create a facebook.js, in this file you put the Facebook-SDK -> inside facebook.js put this
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(); } }); }); }); };
// Load the SDK Asynchronously (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); }(document));
That's it.... do not forget to add -> required on integrating facebook plugin -> required on integrating facebook plugin
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.