Here we'll show you how to go through the Facebook OAuth process in Ruby, which lets any Facebook user log in to Facebook and grant your web app access to their account. Our simple app logs users in and displays some info about their Facebook profile.
In order to follow this example, you'll need Ruby 1.9 (or later) and a web application server listening on http://127.0.0.1:4567. We're going to use Sinatra for this example.
If you don't already have Sinatra installed, you can use the following command:
sudo gem install sinatra
Note: The Sinatra server does not automatically reload when you update your code. Sinatra's official suggestions for working around this are here.
1 Log in to Temboo. If you don't already have an account, you can register for free.
2 If you have not already, download and install the Ruby SDK gem as described in our getting started tutorial.
3 Create a new Facebook app via the Facebook developer console using the Apps menu at the top of the page. Once you've created a new App, click the Settings tab on the left, select + Add Platform, and choose the Website option. Set up your Temboo callback URL by specifying the following URL as your Site URL:
https://ACCOUNT_NAME.temboolive.com/callback/
4 Choose a location for your project and create a file called fboauth.rb there. Add the code below to the fboauth.rb file, making sure to substitute in your Facebook app details.
require 'sinatra' require 'temboo' require 'Library/Facebook' # By default, Sinatra disables sessions. To keep things simple, we'll store # values accessed across pages in global variables. $temboo_session = TembooSession.new('ACCOUNT_NAME', 'APP_NAME', 'APP_KEY') $app_id = 'FB_APP_ID' $app_secret = 'FB_APP_ID_SECRET' $callback_id = '' # Leave this empty to start out. # Landing page with a link to get the OAuth process started. get '/' do 'Log in with <a href="initialize">Facebook</a>.<br />' end get '/initialize' do oauth_init_choreo = Facebook::OAuth::InitializeOAuth.new($temboo_session) # Get an InputSet object for the choreo oauth_init_inputs = oauth_init_choreo.new_input_set() # Set inputs oauth_init_inputs.set_AppID($app_id) oauth_init_inputs.set_ForwardingURL('http://127.0.0.1:4567/finalize') oauth_init_results = oauth_init_choreo.execute(oauth_init_inputs) # Populate the global callback ID. $callback_id = oauth_init_results.get_CallbackID() # Proceed to the authorization URL to grant this app access to your # Facebook info. redirect oauth_init_results.get_AuthorizationURL() end get '/finalize' do # Complete the OAuth process. oauth_final_choreo = Facebook::OAuth::FinalizeOAuth.new($temboo_session) oauth_final_inputs = oauth_final_choreo.new_input_set() oauth_final_inputs.set_AppID($app_id) oauth_final_inputs.set_AppSecret($app_secret) oauth_final_inputs.set_CallbackID($callback_id) oauth_final_results = oauth_final_choreo.execute(oauth_final_inputs) # Using the token obtained in the OAuth process, display user info. user_choreo = Facebook::Reading::User.new($temboo_session) user_inputs = user_choreo.new_input_set() user_inputs.set_AccessToken(oauth_final_results.get_AccessToken()) user_results = user_choreo.execute(user_inputs) user_results.get_Response() end
5 Next, we'll start the Sinatra application server by running the following command from your project location:
ruby fboauth.rb
5 If you installed rerun to monitor your Sinatra server, run the following instead:
rerun 'ruby fboauth.rb'
6 Now you should be able to browse to the following URL:
http://127.0.0.1:4567
7 Click Login with Facebook and go through the OAuth process.
8 Once you've been redirected to Facebook, you can log in and grant the application access to your Facebook account. At this point in the example, you should be redirected back to your application where you'll see the user profile information in JSON format. That's it!
This example includes two main functions for completing the OAuth process:
In the initialize route, we get the Authorization URL and Callback ID:
Below is the function that generates the Callback ID and redirects the user to the Authorization URL:
get '/initialize' do oauth_init_choreo = Facebook::OAuth::InitializeOAuth.new($temboo_session) # Get an InputSet object for the choreo oauth_init_inputs = oauth_init_choreo.new_input_set() # Set inputs oauth_init_inputs.set_AppID($app_id) oauth_init_inputs.set_ForwardingURL('http://127.0.0.1:4567/finalize') oauth_init_results = oauth_init_choreo.execute(oauth_init_inputs) # Populate the global callback ID. $callback_id = oauth_init_results.get_CallbackID() # Proceed to the authorization URL to grant this app access to your # Facebook info. redirect oauth_init_results.get_AuthorizationURL() end
In the finalize route, we run the FinalizeOAuth Choreo and pass the returned access token to the Facebook > Reading > User Choreo to retrieve your user's profile information.
get '/finalize' do # Complete the OAuth process. oauth_final_choreo = Facebook::OAuth::FinalizeOAuth.new($temboo_session) oauth_final_inputs = oauth_final_choreo.new_input_set() oauth_final_inputs.set_AppID($app_id) oauth_final_inputs.set_AppSecret($app_secret) oauth_final_inputs.set_CallbackID($callback_id) oauth_final_results = oauth_final_choreo.execute(oauth_final_inputs) # Using the token obtained in the OAuth process, display user info. user_choreo = Facebook::Reading::User.new($temboo_session) user_inputs = user_choreo.new_input_set() user_inputs.set_AccessToken(oauth_final_results.get_AccessToken()) user_results = user_choreo.execute(user_inputs) user_results.get_Response() end
We're all finished! This Ruby application executes the OAuth flow, and retrieves information about your app's user. We have OAuth support for many of the other APIs in our Library.
Once you've got your code up and running, you're ready to move on and do more. From monitoring your running applications, to moving your generated Temboo code to your preferred development environment and sharing it with colleagues, collaborators and friends - we've got you covered.
We're always happy to help. Just email us at support@temboo.com, and we'll answer your questions.