Extract cookie data in Facebook and Rails 3
I am using Rails 3 and the Facebook Javascript SDK and when I login a cookie is created. I want to be able to extract data from this cookie using Rails.
I got a PHP code that doe开发者_JS百科s the trick but I want to use Rails code. What does the below code written in PHP look like in Rails?
function get_facebook_cookie($app_id, $application_secret) {
$args = array();
parse_str(trim($_COOKIE['fbs_' . $app_id], '\\"'), $args);
ksort($args);
$payload = '';
foreach ($args as $key => $value) {
if ($key != 'sig') {
$payload .= $key . '=' . $value;
}
}
if (md5($payload . $application_secret) != $args['sig']) {
return null;
}
return $args;
}
$cookie = get_facebook_cookie(FACEBOOK_APP_ID, FACEBOOK_SECRET);
Instead of reinventing the wheel, use one of the Ruby gems that support Facebook's API. For example, the equivalent using the Koala gem looks something like:
require 'rubygems'
require 'koala'
FB_APP_ID = "<your-app-id">
FB_APP_SECRET = "<your-app-secret>"
@oauth = Koala::Facebook::OAuth.new(FB_APP_ID, FB_APP_SECRET)
info = @oauth.get_user_info_from_cookies(cookies) # parses and returns the entire hash
puts info.inspect
精彩评论