Devise - Omniauth - Facebook - Getting Extra Permissions
When using devise and omniauth for authentication sometimes you need to require more permissions from facebook. This is useful when you want to later use the authentication code from facebook do such things as posting to the users wall (I recommend the Koala Gem for this).
Within the devise config you will probably already have your facebook omniauth provider setup. After passing the secret key you just need to pass a scope requesting the additional permissions required for your application.
When your user is now setup or authorized from facebook make sure you save the code parameter against the user, this is passed back to your omniauth callbacks controller. Here is an example of mine below.
deffacebook_configYAML.load_file("#{Rails.root}/config/facebook.yml")[Rails.env]enddeffacebook_client(access_token)Koala::Facebook::API.new(access_token)enddefconfigure_facebook_user(authorisation_hash,code)user=User.where(:email=>authorisation_hash.info.email).firstunlessuseruser=User.create(:email=>authorisation_hash.info.email,:first_name=>authorisation_hash.extra.raw_info.first_name,:last_name=>authorisation_hash.extra.raw_info.last_name,:gender=>Gender[authorisation_hash.extra.raw_info.gender.to_sym],:date_of_birth=>nil,:location=>authorisation_hash.extra.raw_info.hometown.name,:about=>authorisation_hash.extra.raw_info.bio,:password=>Devise.friendly_token[0,20])enduser.role=Role[:member]unlessuser.rolebeginuser.token=User.facebook_oauth_client.get_access_token(code)rescue#External api call rescue all!enduser.provider=authorisation_hash.provideruser.uid=authorisation_hash.uiduser.saveuser.confirm!user.remember_me!userenddeffacebook_oauth_clientKoala::Facebook::OAuth.new(User.facebook_config['app_id'],User.facebook_config['secret_key'],"#{User.facebook_config['callback']}/users/auth/facebook/callback")end