facebook authentication using oauth2 gem in rails

July 26, 2010 § 4 Comments

in this section,  i wanna share how to intregrate the Facebook’s new OAuth authorization in Ruby On Rails, using oauth2 gem.

let’s get started !

first, install the oauth2 gem, you can get it from here

http://github.com/intridea/oauth2

then create a controller call oauth_controller.rb

class OauthController < ApplicationController

def start
redirect_to client.web_server.authorize_url(
:redirect_uri =&gt; oauth_callback_url, :scope =&gt; 'email'
)
end

def callback
access_token = client.web_server.get_access_token(
params[:code], :redirect_uri =&gt; oauth_callback_url
)

session[:fb_user] = JSON.parse access_token.get('/me')
# in reality you would at this point store the access_token.token value as well as
# any user info you wanted

redirect_to :controller =&gt; :users, :action =&gt; :login_process_facebook, :token =&gt; access_token.token

end

protected

def client
facebook_settings = YAML::load(File.open("#{RAILS_ROOT}/config/oauth.yml"))
@client ||= OAuth2::Client.new(
"#{facebook_settings[RAILS_ENV]['application_id']}", "#{facebook_settings[RAILS_ENV]['secret_key']}",
:site =&gt; 'https://graph.facebook.com'
)
end
end

then just add the configuration of your facebook apps like apps_id, api_key, secret_id, callback_url

anyway, if we insert manually the apps_id, api_key, etc. to the controller i think is too tricky.. so lets make it simple

tips from me.. you can add a configuration file in your rails app config folder.

create a oauth.yml (or anything you want to name it )

development:
application_id: xxxxxxxxxxxx
api_key: xxxxxxxxxxxxxxxxxxxxxxxx
secret_key: xxxxxxxxxxxxxxxxxxxxxxxx
canvas_page_name: MyApps
callback_url: your_callback_url
pretty_errors: true
set_asset_host_to_callback_url: true
tunnel:
public_host_username:
public_host:
public_port: 4007
local_port: 3000
server_alive_interval: 0

production:
application_id: xxxxxxxxxxxx
api_key: xxxxxxxxxxxxxxxxxxxxxxxx
secret_key: xxxxxxxxxxxxxxxxxxxxxxxx
canvas_page_name: MyApps
callback_url: your_callback_url
pretty_errors: true
set_asset_host_to_callback_url: true
tunnel:
public_host_username:
public_host:
public_port:
local_port:
server_alive_interval: 0

after your create it, you can call the config like this.

YAML::load(File.open("#{RAILS_ROOT}/config/oauth.yml")

so now you already have the oauth controller and the oauth configuration file. at here.. you almost done !

last step is creating the facebook connect button, you can create it manually or using the facebook JavaScript SDK,

create manually using a costum images :

<%= link_to image_tag("fb_button.gif"), :controller => 'oauth', :action => 'start' %>

using facebook JavaScript SDK:

<fb:login-button onlogin='window.location="/oauth/callback"' perms="email" ></fb:login-button>

<div style="float:right">
  <div style="float:left;padding-right:10px">
    <fb:login-button onlogin='window.location="/oauth/start"' perms="email" >
    </fb:login-button>
  </div>
  <div style="float:left">
  </div>
</div>

and done !

 

now your webservice can connect to facebook apps using oauth2, next you can catch the user data like name, email, etc.

i will write it he next post 🙂

Advertisements

Tagged: , , , , , , , ,

§ 4 Responses to facebook authentication using oauth2 gem in rails

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

What’s this?

You are currently reading facebook authentication using oauth2 gem in rails at Kubid on rails blog.

meta

%d bloggers like this: