Get started with Temboo and Rails


Here we'll show you how to get started with Temboo in a Rails app. We'll use Facebook OAuth, which lets any Facebook user log in to Facebook and grant your app access to their account, as an example to illustrate everything you need to know about how to use Temboo with Rails. 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 Rails web application server listening on http://0.0.0.0:3000.

If you don't already have Rails installed, you can use the following command:

sudo gem install rails

Run our Facebook OAuth Example

1 Log in to Temboo. If you don't already have an account, you can register for free here.

2 If you haven't 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 run the following command in your terminal:

rails new oauth_demo

5 Run the following command from the new project location:

bin/rails generate controller facebook_oauth index initialize_oauth finalize_oauth

6 Add gem 'temboo' and the SDK version number to your Gemfile. You'll need to replace x.x.x below with something like 2.6.0 i.e., the version number of the Temboo Gem that you downloaded from our website:

gem 'temboo', '<= x.x.x'

7 Run the bundle install command:

bundle install

8 Populate the views:

/oauth_demo/app/views/facebook_oauth/index.html.erb

<h1>
This is a simple app that demonstrates using the Temboo SDK to perform Facebook
OAuth authentication.
</h1>
<p><a href="/facebook_oauth/initialize_oauth">Login with Facebook</a>

/oauth_demo/app/views/facebook_oauth/initialize_oauth.html.erb

<h1>FacebookOauth#initialize</h1>
<p>Callback ID: <%= @callbackID %></p>
<p>Authorization URL: <%= @authorizationURL %>

/oauth_demo/app/views/facebook_oauth/finalize_oauth.html.erb

<h1>FacebookOauth#finalize</h1>
<p>Success! User profile: <%= @userData %></p>
<p><a href="/facebook_oauth/index">Start Over</a>

8 Populate the Rails controller, and set your Temboo and Facebook constants:

/oauth_demo/app/controllers/facebook_oauth_controller.rb

require 'temboo'
require 'Library/Facebook'

class FacebookOauthController < ApplicationController

    # Temboo account name and application key; get these from
    # https://www.temboo.com/account/applications/ (after registering a free account)
    TEMBOO_ACCOUNT_NAME = 'xxxxxxx'
    TEMBOO_APP_KEY_NAME = 'xxxxxxx'
    TEMBOO_APP_KEY = 'xxxxxxx'
    # Facebook application ID and secret. Make sure to register the app following the
    # instructions at https://www.temboo.com/library/Library/Facebook/OAuth/
    FACEBOOK_APP_ID = 'xxxxxxx'
    FACEBOOK_APP_SECRET = 'xxxxxxx'
    # The location of the 'finalize OAuth' controller for this app
    FINALIZE_OAUTH_CONTROLLER = "http://0.0.0.0:3000/facebook_oauth/finalize_oauth"
    TEMBOO_SESSION = TembooSession.new(TEMBOO_ACCOUNT_NAME, TEMBOO_APP_KEY_NAME, TEMBOO_APP_KEY)

    def index
        # Nothing to do here; index is just a static HTML page
    end

    def initialize_oauth
        # Instantiate the OAuth.InitializeOAuth choreo
        initializeOAuthChoreo = Facebook::OAuth::InitializeOAuth.new(TEMBOO_SESSION)
        # Get an InputSet object for the choreo
        initializeOAuthInputs = initializeOAuthChoreo.new_input_set()
        # Set inputs
        initializeOAuthInputs.set_AppID(FACEBOOK_APP_ID)
        initializeOAuthInputs.set_ForwardingURL(FINALIZE_OAUTH_CONTROLLER)
        # Execute Choreo
        initializeOAuthResults = initializeOAuthChoreo.execute(initializeOAuthInputs)
        @callbackID = initializeOAuthResults.get_CallbackID()
        @authorizationURL = initializeOAuthResults.get_AuthorizationURL()
        # Store the callback ID in a cookie; we'll need this during
        # the finalizeOAuth process
        cookies[:tembooCallbackID] = @callbackID
        # Redirect the user to the authorization URL
        redirect_to @authorizationURL
    end

    def finalize_oauth
        # Grab the callback ID out of the cookie
        @retrievedCallbackID = cookies[:tembooCallbackID]
        # Instantiate the OAuth.FinalizeOAuth Choreo, using a previously instantiated TembooSession object
        finalizeOAuthChoreo = Facebook::OAuth::FinalizeOAuth.new(TEMBOO_SESSION)
        # Get an InputSet object for the choreo
        finalizeOAuthInputs = finalizeOAuthChoreo.new_input_set()
        # Set inputs
        finalizeOAuthInputs.set_CallbackID(@retrievedCallbackID)
        finalizeOAuthInputs.set_AppID(FACEBOOK_APP_ID)
        finalizeOAuthInputs.set_AppSecret(FACEBOOK_APP_SECRET)
        # Execute Choreo
        finalizeOAuthResults = finalizeOAuthChoreo.execute(finalizeOAuthInputs)
        # Get the Facebook access token
        @accessToken = finalizeOAuthResults.get_AccessToken();

        # Instantiate the Reading.User Choreo, using a previously instantiated TembooSession object
        userChoreo = Facebook::Reading::User.new(TEMBOO_SESSION)
        # Get an InputSet object for the choreo
        userInputs = userChoreo.new_input_set()
        # Set inputs
        userInputs.set_AccessToken(@accessToken);
        # Execute Choreo
        userResults = userChoreo.execute(userInputs)
        # Get user profile data from Response
        @userData = userResults.get_Response();
        # Store the access token in a cookie, for reuse
        cookies[:facebookAccessToken] = @accessToken
    end
end

9 Next, we'll start the Rails application server by running the following command from your project location:

bin/rails server

10 Now you should be able to browse to the following URL:

http://0.0.0.0:3000/facebook_oauth/index

11 Click Login with Facebook and go through the OAuth process.

12 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! The process is example the same for using any of the other 2000+ Choreos in our Library.

Taking a closer look at the code

This example includes two main functions for completing the OAuth process:

In the initialize method, 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:

    def initialize_oauth
        # Instantiate the Initialize OAuth choreo
        initializeOAuthChoreo = GitHub::OAuth::InitializeOAuth.new(TEMBOO_SESSION)
        # Get an InputSet object for the choreo
        initializeOAuthInputs = initializeOAuthChoreo.new_input_set()
        # Set inputs
        initializeOAuthInputs.set_ClientID(GITHUB_CLIENT_ID)
        initializeOAuthInputs.set_ForwardingURL(FINALIZE_OAUTH_CONTROLLER)
        # Execute Choreo
        initializeOAuthResults = initializeOAuthChoreo.execute(initializeOAuthInputs)
        @callbackID = initializeOAuthResults.get_CallbackID()
        @authorizationURL = initializeOAuthResults.get_AuthorizationURL()
        # Store the callback ID in a cookie; we'll need this during
        # the finalizeOAuth process
        cookies[:tembooCallbackID] = @callbackID
        # Redirect the user to the authorization URL
        redirect_to @authorizationURL
    end

In the finalize_oauth method, we run the FinalizeOAuth Choreo and pass the returned access token to the Facebook > Reading > User Choreo to retrieve your user's profile information.

    def finalize_oauth
        # Grab the callback ID out of the cookie
        @retrievedCallbackID = cookies[:tembooCallbackID]
        # Instantiate the OAuth.FinalizeOAuth Choreo, using a previously instantiated TembooSession object
        finalizeOAuthChoreo = Facebook::OAuth::FinalizeOAuth.new(TEMBOO_SESSION)
        # Get an InputSet object for the choreo
        finalizeOAuthInputs = finalizeOAuthChoreo.new_input_set()
        # Set inputs
        finalizeOAuthInputs.set_CallbackID(@retrievedCallbackID)
        finalizeOAuthInputs.set_AppID(FACEBOOK_APP_ID)
        finalizeOAuthInputs.set_AppSecret(FACEBOOK_APP_SECRET)
        # Execute Choreo
        finalizeOAuthResults = finalizeOAuthChoreo.execute(finalizeOAuthInputs)
        # Get the Facebook access token
        @accessToken = finalizeOAuthResults.get_AccessToken();

        # Instantiate the Reading.User Choreo, using a previously instantiated TembooSession object
        userChoreo = Facebook::Reading::User.new(TEMBOO_SESSION)
        # Get an InputSet object for the choreo
        userInputs = userChoreo.new_input_set()
        # Set inputs
        userInputs.set_AccessToken(@accessToken);
        # Execute Choreo
        userResults = userChoreo.execute(userInputs)
        # Get user profile data from Response
        @userData = userResults.get_Response();
        # Store the access token in a cookie, for reuse
        cookies[:facebookAccessToken] = @accessToken
    end

What's Next?

We're all finished! This Rails 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.

Need help?

We're always happy to help. Just email us at support@temboo.com, and we'll answer your questions.


Back