首先您需要添加setup: true
能够升级服务的权限列表:
Devise.setup do |config|
config.omniauth :facebook, ENV['FACEBOOK_KEY'], ENV['FACEBOOK_SECRET'],
:scope => 'email,offline_access,user_about_me',
:setup => true
end
在 中添加两条路线routes.rb
:
devise_scope :user do
get '/users/auth/:provider/upgrade' => 'omniauth_callbacks#upgrade', as: :user_omniauth_upgrade
get '/users/auth/:provider/setup', :to => 'omniauth_callbacks#setup'
end
第一条路线是用户应该通过使用链接到的位置user_omniauth_upgrade_path(:facebook)
。第二个设置路由是omniauth将在内部调用的回调,我们可以使用它来更改范围参数。
这些进入omniauth_callbacks_controller.rb
:
def upgrade
scope = nil
if params[:provider] == "facebook"
scope = 'email,offline_access,user_about_me,publish_stream'
end
redirect_to user_omniauth_authorize_path(params[:provider]), flash: {scope: scope}
end
当您setup: true
在omniauth 配置setup_path
中指定时,默认情况下会调用该配置。我们将使用它来更改策略中的默认范围。将此添加到omniauth_callbacks_controller.rb
:
def setup
request.env['omniauth.strategy'].options['scope'] = flash[:scope] || request.env['omniauth.strategy'].options['scope']
render :text => "Setup complete.", :status => 404
end
最后,在您的视图中,您可以添加:
<%= link_to "Upgrade Access", user_omniauth_upgrade_path(:facebook) %>
资料来源:
http ://willschenk.com/setting-up-devise-with-twitter-and-facebook-and-other-omniauth-schemes-without-email-addresses/#passing-dynamic-scopes-to-omniauth