This SDK is based on the offical Facebook PHP SDK
To start using this gem you need to include it in your Rails4 app Gemfile:
#gem RailsBook
gem 'railsbook', github: 'bukk530/railsbook'Save your Gemfile and run:
bundle installNow that you have installed RailsBook you need to configure your Facebook App!
Do:
rails g rails_book:configto generate a config/facebook_app.yml configuration file.
Open config/facebook_app.yml, it should look like this:
app_id: "YOUR APP ID"
app_secret: "YOUR SECRET"Now go to your Facebook application and replace the App Id and Secret in your config file
If you use Git, do:
git add config/initializers/facebook.rb
git add config/facebook_app.ymlNow you should be able to play with the SDK!
Ok let's start with a simple login page:
routes.rb
Rails.application.routes.draw do
root "fb_login_controller#show_login"
get "/login", to: "fb_login_controller#login", as: :login
endfb_login_controller.rb
class FbLoginController < ApplicationController
include RailsBook
# Initialize our helper (in a future version this will be a real rails helper)
before_action do
@facebook_login_helper = FacebookRedirectLoginHelper.new login_url, session, params
end
# In this page we will ask the user to log in via Facebook
def show_login
@facebook_login_url = @facebook_login_helper.get_login_url
end
# This page is called back from Facebook after the user logged in
def login
facebook_session = @facebook_login_helper.get_session_from_redirect
@user = FacebookRequest.new(facebook_session, 'GET', '/me').execute.response
end
endshow_login.html.erb
<a href="<%= @facebook_login_url %>">Login via Facebook!</a>login.html.erb
<h1>Halo <%= @user['name'] %></h1>