omniauth snippets

How to implement multiple OmniAuth strategies for Devise

Tagged authentication, facebook, devise, omniauth  Languages ruby

This snippet is useful if you're using Devise and OmniAuth for authentication and need to support authentication with, for example, two separate Facebook applications.

First, put this in, for example, lib/omniauth_i18n.rb:

# The "name" method defines the URL, so the URL becomes /users/auth/facebook_x
module OmniAuth::Strategies
   class FacebookX < Facebook
    def name 
  class FacebookY < Facebook
    def name 

Configure each strategy separately in config/initializers/devise.rb:

config.omniauth :facebook_x, 'yyy', 'yyy'
config.omniauth :facebook_y, 'zzz', 'zzz'

This was tested with devise 1.1, oa-auth 0.1.6 and oauth 0.4.4, and will probably break sometime in the future. OmniAuth supports dynamic strategies

How to post a message to the Facebook wall with OmniAuth/Devise

Tagged facebook, omniauth, devise  Languages ruby

In your OmniAuth/Devise-callback controller add this line:

session['fb_access_token'] = omniauth['credentials']['token']

This will save the auth_token, which we’ll need for accessing the Facebook API.

Next, in your Devise initializer, usually config/initializer/devise.rb, request additional permissions:

config.omniauth :facebook, 'heh', 'lol', :scope => 'publish_stream,email,offline_access,manage_pages'

The offline_access permission means you can use the token even if the user logs out.

Now if a user logs in you’ll have access to the user’s Facebook access_token, which can be used to post to his wall.

api_key = 'heh'
api_secret = 'lol'

client =, api_secret, :site => '')
token =, session['fb_access_token'])'/me/feed', :message => "testing")

To understand how it all works, read about Facebook permissions, the Graph API and the documentation on the post command.

Getting the Access Token

You can also use this URL to get a permanent access_token:<app_key>&client_secret=<app_secret>

Posting links

Use to post links.

Posting to a page

When posting to a page you need to change "me" to the page name:'/acmexxx/post', :message => "testing")

If you need to post to the page as the page itself, not the admin user, you need to get the access_token for the page.

First you need to add the manage_pages permission:

config.omniauth :facebook, 'heh', 'lol', :scope => 'publish_stream,email,offline_access,manage_pages'

You should now login as the page admin and get a new access_token, which you can then use to get a list of accounts and pages you are the administrator for:<admin_access_token>

You should see the access_token for each page listed in the response, use it to post to the page as the page.