How to work with koala gem for facebook integration with Ruby On Rails:- I am just giving a simple example with koala gem :-)
How to work with koala gem for facebook integration with Ruby On Rails:-
I am just giving a simple example with koala gem :-)
1. Create a ROR project
2. Install Koala gem and make necessorry changes into your enviroment.rb file.
=> config.gem "koala"
=> after the "end" tag of your environment file add the folowing line
require 'rubygems'
require 'koala'
# for Facebook Connect
FB_APP_KEY = "your app key"
FB_APP_SECRET = "your app secret key"
FB_APP_ID = "your app id"
3. In your HTML file or layout file add the following line
< xmlns="http://www.w3.org/1999/xhtml" fb="http://www.facebook.com/2008/fbml">
and in your (controller)=>user/(action)login -> View file i.e login.html.erb add the following
4. For Facebook login button add the following
< perms="email,user_birthday">< /fb:login-button>
5. Now after the user get login through Flogin. the user will be redirect to account(action) of user(controller). SO add the following line of code to see some action ..
def account
@oauth = Koala::Facebook::OAuth.new(FB_APP_ID, FB_APP_SECRET, 'http://192.168.1.50:3000/')
user_id = @oauth.get_user_from_cookies(cookies) # gets the user's ID
user_info = @oauth.get_user_info_from_cookies(cookies) # parses and returns the entire hash
access_token = user_info["access_token"]
# generate authenticating URL
auth_url = @oauth.url_for_oauth_code
# hit the authenticating URL url generated by @oauth.url_for_oauth_code it will redirect to ur call back page with 'code' parameter. copy & paste the code to code variable
code = "b5b61e003e4d0565285e68aa-1160256180|v2DFQ5gUWKxucmYuqiN1h847N4s"
# fetch the access token once you have the code
oauth_access_token = @oauth.get_access_token(code)
# initialize a Graph API connection, for instance
@graph = Koala::Facebook::GraphAPI.new(oauth_access_token)
#Fetch user profile data
profile = @graph.get_object("me")
#Fetch user's friend list
friends = @graph.get_connections("me", "friends")
#Write into user's wall or friend feed
@graph.put_object("me", "feed", :message => "I am writing on my wall!")
end
6. Thats all :-)
I am just giving a simple example with koala gem :-)
1. Create a ROR project
2. Install Koala gem and make necessorry changes into your enviroment.rb file.
=> config.gem "koala"
=> after the "end" tag of your environment file add the folowing line
require 'rubygems'
require 'koala'
# for Facebook Connect
FB_APP_KEY = "your app key"
FB_APP_SECRET = "your app secret key"
FB_APP_ID = "your app id"
3. In your HTML file or layout file add the following line
< xmlns="http://www.w3.org/1999/xhtml" fb="http://www.facebook.com/2008/fbml">
and in your (controller)=>user/(action)login -> View file i.e login.html.erb add the following
< id="fb-root">< /div>
< src="http://connect.facebook.net/en_US/all.js">
<>
FB.init({
appId : '131557463566628',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
FB.Event.subscribe('auth.sessionChange', function(response) {
if (response.session) {
location.href = '/user/account';
// A user has logged in, and a new cookie has been saved
} else {
location.href = '/user/login';
// The user has logged out, and the cookie has been cleared
}
});
< /script>
FB.init({
appId : '131557463566628',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
FB.Event.subscribe('auth.sessionChange', function(response) {
if (response.session) {
alert('hi');
location.href = '/user/account';
// A user has logged in, and a new cookie has been saved
} else {
alert('hello');
location.href = '/user/login';
// The user has logged out, and the cookie has been cleared
}
});
FB.init({
appId : '131557463566628',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
FB.Event.subscribe('auth.sessionChange', function(response) {
if (response.session) {
location.href = '/user/account';
// A user has logged in, and a new cookie has been saved
} else {
location.href = '/user/login';
// The user has logged out, and the cookie has been cleared
}
});
4. For Facebook login button add the following
5. Now after the user get login through Flogin. the user will be redirect to account(action) of user(controller). SO add the following line of code to see some action ..
def account
@oauth = Koala::Facebook::OAuth.new(FB_APP_ID, FB_APP_SECRET, 'http://192.168.1.50:3000/')
user_id = @oauth.get_user_from_cookies(cookies) # gets the user's ID
user_info = @oauth.get_user_info_from_cookies(cookies) # parses and returns the entire hash
access_token = user_info["access_token"]
# generate authenticating URL
auth_url = @oauth.url_for_oauth_code
# hit the authenticating URL url generated by @oauth.url_for_oauth_code it will redirect to ur call back page with 'code' parameter. copy & paste the code to code variable
code = "b5b61e003e4d0565285e68aa-1160256180|v2DFQ5gUWKxucmYuqiN1h847N4s"
# fetch the access token once you have the code
oauth_access_token = @oauth.get_access_token(code)
# initialize a Graph API connection, for instance
@graph = Koala::Facebook::GraphAPI.new(oauth_access_token)
#Fetch user profile data
profile = @graph.get_object("me")
#Fetch user's friend list
friends = @graph.get_connections("me", "friends")
#Write into user's wall or friend feed
@graph.put_object("me", "feed", :message => "I am writing on my wall!")
end
6. Thats all :-)
thanks for this but do you have missing code up there for the views?
ReplyDeletecan't seem to see anything.
Yeh i will update it very soon. Thanks to visit.
ReplyDeleteI have to say, in a world full of confusing and terrible facebook tutorials, yours was one of the easiest and the best. 1 correction though:
ReplyDeletefb login button is:
Thanks a bunch though!!!
Can you add the full example of the environment.rb file? I don't have an "end" tag.
ReplyDeleteI am still getting the message:
`require': no such file to load -- koala (LoadError)
I did 'gem install koala' which executed with no problems. I added the lines below to my environment.rb file:
config.gem "koala"
require 'rubygems'
require 'koala'
Hi,
ReplyDeleteThanks for a good article.
I am using the same in my rails 3 cod.e My problem is I can get user id from
@oauth.get_user_from_cookies(cookies)
but I am getting Faraday connection failed error when I have
@oauth.get_user_info_from_cookies(cookies)
I have tried at many different ways but same error comes at this sentence.
Do you have any idea why this happens?
Any will be appreciated.
Thanks