With Facebook’s Graph API and the creation of the Open Graph protocol, it is now easier then ever before to read and write data from facebook to rails application and back to the “social graph”.
Here is a few of the possibilities:
-
You could turn your webpage into a fully featured Facebook like page, just like if you were inside Facebook.
-
You can give your users the ability to sign in with their Facebook credentials and customize their experience with parameters taken from their Facebook profiles.
-
You could add a Like button to every object in your page such as images, songs, articles, etc., and tell your users which friends of theirs have liked your content.
Steps to Create Application and Fetch Information
A facebook app_id, and api_secret_key is needed
go to the link
https://developers.facebook.com/apps
1. Create new app on facebook
-
Give app name(example: sample_app)
-
then we need to fill following information
-
App domain: localhost
-
website url: http://localhost/3000/
On the same page we can find App Id & App Secret . It looks like
App ID: 378271044441102
App Secret: 567772fd2bef4dda7a404b02r4567c758
2. Create Rails application
-
rails new facebook_app
-
sudo gem install fb_graph
-
add to Gemfilegem “fb_graph”
-
sudo bundle install
-
create index page to fb_loginrails generate controller pages index
-
set index to root page in config/routes.rbroot :to => “pages#home”
-
create authentication for app(simple authentication)
-
creating facebook controller and facebook model with :Identifier => string:access_token =>string
-
rails generate scaffold facebook identifier:string access_token:string
-
rails generate controller dashboard show
- In pages controller redirect the page to dashboard:show if it is authenticated alreadycontroller/pages_controller.rb
def index
redirect_to dashboard_url if authenticated?
end
If not authenticated then root_path will be loaded ie. pages/index, create fb_login in this page to authebticate.
<% if authenticated? %>
<p><%= link_to “Logout”, facebook_path, :method => :delete %></p>
<% else %>
<fb:login-button length=”long” onlogin=”location.href = ‘<%= facebook_path %>’” scope=”<%=
Facebook.config[:scope] %>”> </fb:login-button>
<% end %>
3. Paste the following script code(for facebook login button)
<script src=”http://connect.facebook.net/en_US/all.js”></script>
<script>
FB.init({
appId: “<%= Facebook.config[:client_id] %>”,
cookie: true,
xfbml: true,
oauth: true,
status: true
});
</script>
<script type=”text/javascript”>
$(function () {
<% if flash[:error] %>
$.gritter.add({
title: “<%= flash[:error][:title] %>”,
text : “<%= flash[:error][:message] %>”,
image: “<%= flash[:error][:image] %>”,
time : 5000
});
<% elsif flash[:notice] %>
$.gritter.add({
title: “<%= flash[:notice][:title] %>”,
text : “<%= flash[:notice][:message] %>”,
image: “<%= flash[:notice][:image] %>”,
time : 3000
});
<% end %>
});
</script>
The above code is used to authenticate user through facebook, after authenticating user should redirect to dashboard url to show his information fetched from facebook. some facebook settings should be code in facebook model, and controller
In controller/facebook_controller.rb.
before_filter :require_authentication, :only => :destroy
rescue_from Rack::OAuth2::Client::Error, :with => :oauth2_error
# handle Facebook Auth Cookie generated by JavaScript SDK
def show
auth = Facebook.auth.from_cookie(cookies)
authenticate Facebook.identify(auth.user)
redirect_to dashboard_url
end
# handle Normal OAuth flow: start
def new
client = Facebook.auth(callback_facebook_url).client
redirect_to client.authorization_uri(
:scope => Facebook.config[:scope]
)
end
# handle Normal OAuth flow: callback
def create
client = Facebook.auth(callback_facebook_url).client
client.authorization_code = params[:code]
access_token = client.access_token! :client_auth_body
user = FbGraph::User.me(access_token).fetch
authenticate Facebook.identify(user)
redirect_to dashboard_url
end
def destroy
unauthenticate
redirect_to root_url
end
private
def oauth2_error(e)
flash[:error] = {
:title => e.response[:error][:type],
:message => e.response[:error][:message]
}
redirect_to root_url
end
Read More: Railscarma.com