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:

# https://github.com/intridea/omniauth/wiki/Auth-Hash-Schema
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 = OAuth2::Client.new(api_key, api_secret, :site => 'https://graph.facebook.com')
token = OAuth2::AccessToken.new(client, session['fb_access_token'])
token.post('/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:

https://graph.facebook.com/oauth/access_token?type=client_cred&client_id=<app_key>&client_secret=<app_secret>

Posting links

Use http://developers.facebook.com/docs/reference/api/link/ to post links.

Posting to a page

When posting to a page you need to change "me" to the page name:

token.post('/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:

https://graph.facebook.com/me/accounts?access_token=<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.