facebook snippets

How to get Facebook like and Twitter share counts with Ruby

Tagged facebook, twitter, share, like  Languages ruby
def facebook_likes(url)
    data = open("http://graph.facebook.com/?ids=#{URI.escape(url)}").read
    data = JSON.parse(data)
    data[url]['shares']
  end

  def twitter_shares(url)
    data = open("http://urls.api.twitter.com/1/urls/count.json?url=#{URI.escape(url)}").read
    data = JSON.parse(data)
    data['count']
  end

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 
       :facebook_x
    end 
  end
  class FacebookY < Facebook
    def name 
       :facebook_y
    end 
  end
end

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:

# 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.