There are many ways to use the same gem , once of the ways found by me.
Koala is a Facebook library for Ruby, supporting the Graph API, the REST API, realtime updates, test users, and OAuth validation.
1) In this example first will create an application in facebook, to get a pair of keys
For that we have to signin into our facebook account, who is admin of that fan page
and go to https://developers.facebook.com
and can Create New App and also specify in ‘App on Facebook’ tab the Canvas URL and Secure Canvas URL. Here we will get App ID and App Secret.
2) Next step will be add
gem “koala”
in bundler and bundle install
3) It is required to provide Facebook an OAuth token to prove the authenticity. Koala provides an OAuth class for that process:
session[‘oauth’] = Koala::Facebook::OAuth.new(‘App ID’, ‘App Secret’, callback_url)
App ID and App Secret are values which are provided, on creation of the application in facebook, specified in the above step.
This above statement will generate a facebook signin page, where signin credentails, of the fan page admin and application admin, where both the fan page and application will belong to the same account. ‘callback_url’ is the expected url to which after successful signin should be redirected to.
4) It will be required to add the callback url in routes.rb file as follows
match “callback_url” => “controllername#facebookMethod”
5)@oauth can be used to generate the URL for the authentication code. In this can include :permissions to post on fan pages as follows
@oauth = session[‘oauth’].url_for_oauth_code(:permissions => “manage_pages,publish_stream”)
6) Once the admin has logged into Facebook and granted the required permissions, he will be redirected to the callback url; the code, provided by Facebook, will be in that request’s GET parameters. Now can generate the URL that will yield up the access token, as follows:
redirect_to @oauth
7) The facebookMethod will be responsible for posting the required data on fanpage
@access_token_info = session[‘oauth’].get_access_token_info(params[:code])
This will give details such as expires and access token from the code which have been returned after the signin.
8) The Graph API is made up of the objects in Facebook like people, pages, events, photos and the connections between them like friends, photo tags. It is required to create a API object that can be used to make calls. The access token to access private or restricted data is passed along with it.
@graph = user = Koala::Facebook::API.new(@access_token_info[‘access_token’])
9) It is required to get the fan page id
which can be acquired as specified in the following link
http://www.kendamorrison.com/how-to-find-your-facebook-fan-page-id/
10) now using method get_page_access_token can get the page’s access token to act as the page.
page_token = user.get_page_access_token(‘167102573413460’)[‘access_token’]
11) Can create page API object that can be used to make calls. The access token to access private or restricted data is passed along with it.
page = Koala::Facebook::API.new(page_token)
12)
page.put_wall_post(“#{post_body}”,{ “name” => “yourpage”, “link” => “http://yourpage.com”,”caption” => “#{post_caption}”,})
Can look into further details of put_wall_post in the following link
http://rubydoc.info/github/arsduo/koala/master/Koala/Facebook/GraphAPIMethods#put_wall_post-instance_method
In total, the code will look in the controller like this
def facebookMethod
@access_token_info = session[‘oauth’].get_access_token_info(params[:code])
user = Koala::Facebook::API.new(@access_token_info[‘access_token’])
page_token = user.get_page_access_token(‘page_id’)[‘access_token’]
page = Koala::Facebook::API.new(page_token)
page.put_wall_post(“#{post_body}”,{ “name” => “Youmitter”, “link” => “http://yourpage.com”,”caption” => “#{post_caption}”,})
end