This post will introduce the usage of fb_graph Gem with your existing Rails Application.fb_graph has already done a lot for you guys to exploit the FB API features with your Rails Application , you can have an eye on this fb_graph Gem before going through this post.
Suppose you have a blogger kind of application, and when you compose a new post , you would like to share the same in your Facebook wall, and also would like to give the same option for all the users ,those who liked your post and would like to share the same in their Facebook Wall.
I think now almost all the websites are using this feature to increase their user traffic , to popularize their sites, Since Facebook turned out to be a great option for advertising and marketing your site content.
One day i also felt the same and after one complete days of effort ..... i was successful. I thought i should share the same for you guys to reduce your one days effort ( or may be less ) for implementing this FB share option.
I hope i am not boring you , anyway stop to all kind of rubbish introduction and lets start coding ..
As usual first of all add fb_graph Gem to your Gemfile
gem 'fb_graph'
After that we have to create 2 actions for authenticating the User to Facebook and After authenticating the user we need a Callback action and this will share the Contents to that particular Users Facebook Wall.
Here we start from the Route file , mention your roots and controller actions here.
route file
Before get in to the serious part , i would like to tell you more about How i implemented this or what was my requirement.
Like i said before , suppose we have a blogger application , we are going to display the FB share button just below each post.
Requirements
Suppose you have a blogger kind of application, and when you compose a new post , you would like to share the same in your Facebook wall, and also would like to give the same option for all the users ,those who liked your post and would like to share the same in their Facebook Wall.
I think now almost all the websites are using this feature to increase their user traffic , to popularize their sites, Since Facebook turned out to be a great option for advertising and marketing your site content.
One day i also felt the same and after one complete days of effort ..... i was successful. I thought i should share the same for you guys to reduce your one days effort ( or may be less ) for implementing this FB share option.
I hope i am not boring you , anyway stop to all kind of rubbish introduction and lets start coding ..
As usual first of all add fb_graph Gem to your Gemfile
gem 'fb_graph'
After that we have to create 2 actions for authenticating the User to Facebook and After authenticating the user we need a Callback action and this will share the Contents to that particular Users Facebook Wall.
Here we start from the Route file , mention your roots and controller actions here.
route file
match "/fb_share/auth" => "fb_share#auth" , :method => :get , :as => :fb_auth match "/fb_share/callback" => "fb_share#callback" , :method => :get , :as => :fb_callback
Before get in to the serious part , i would like to tell you more about How i implemented this or what was my requirement.
Like i said before , suppose we have a blogger application , we are going to display the FB share button just below each post.
Requirements
- When user clicks on the share button, it should redirect the user to Facebook login, and after allowing the permission to post , The content should post on the Users FB Wall.
- Contents should be,
- Title of my Blogpost.
- Title should be a link to my Blog Posts detail page.
- Small Descrition.
- Thumbnail Image (If you want ).
- When User clicks on share button, We should call an action and here that probably be an Auth Action.
- But after authorizing the user , Facebook will redirect the user back to our Applications Callback action . So at that time , from where will we able to get the Contents like (Title , Description , URL, Image path) ?
- For that reason , i just implemented a cookie for storing all these information .
- When User Clicks on share button , We will pass all these information to Auth action.We Will Assign all these details to Each cookie.
- After Authenticating the User with (Key , Secret) pair, Facebook will redirect the user back to the Callback Action with Authorization code.
- Then we will call the feed action provided by the FB gem and will assign all these contents like ( Title, Description , image path & URL) from the cookie .
- The Rest of the things you dont need to care about . FB Gem will do it for you.
FBShare Controller Actions
class FbShareController < ApplicationController def auth cookies["title"] = { :value => "#{params[:title]}", :expires => 1.minute.from_now } cookies["url"] = { :value => "#{params[:url]}", :expires => 1.minute.from_now } cookies["desc"] = { :value => "#{params[:desc]}", :expires => 1.minute.from_now } @client = client redirect_to @client.authorization_uri( :scope => [ :offline_access , :publish_stream] ) end def callback @client = client @client.authorization_code = params[:code] access_token = @client.access_token! :client_auth_body # => Rack::OAuth2::AccessToken me = FbGraph::User.me(access_token) title = cookies["title"] url = cookies["url"] desc = cookies["desc"] me.feed!( :message => title , :picture => / Your Image path / , :link => url, :name => title, :description => desc ) redirect_to root_path ( You Application Action path After sharing the content to FB) end private def client key = "APP KEY" secret = "SECRET KEY" fb_auth = FbGraph::Auth.new(key, secret) client = fb_auth.client client.redirect_uri = "http://#{request.host_with_port}/fb_share/callback" client end endCheckout my Twitter & Linkedin Share posts also ...
hi nice post can you extend it with devise
ReplyDeleteYou can refer this post http://sreeharikmarar.blogspot.in/2013/01/omniauth-devise-authentication-using.html for Omniauth Devise authentication using Facebook, google & twitter.
DeleteThis comment has been removed by the author.
Deletehi great post sree very helpfull for all ror guys, thanks and continue your posts
ReplyDeleteAttrRequired::AttrMissing in FbShareController#callback
ReplyDeletegetting error while click on auth link for share
AttrRequired::AttrMissing in FbShareController#callback
ReplyDeletegetting error while click on auth link for share
Hello I am so delighted I located your blog, I really located you by mistake, while I was watching on google for something else, Anyways I am here now and could just like to say thank for a tremendous post and a all round entertaining website. Please do keep up the great work. www.dynamicmarketing.sg
ReplyDelete