简体   繁体   English

Devise + omniauth-facebook添加权限

[英]Devise + omniauth-facebook Add permissions

we are letting the users sign up with minimum permissions like this: 我们让用户以最低权限注册,如下所示:

Devise.setup do |config|
  config.omniauth :facebook, ENV['FACEBOOK_KEY'], ENV['FACEBOOK_SECRET'],
              :scope => 'email,offline_access,user_about_me'
end

We do this to increase signup rate (the less permissions you ask for the higher the conversion). 我们这样做是为了提高注册率(您要求转换率越高的权限越少)。

But later when for example the user wants to fb share something we need the publish_stream permission. 但是后来当例如用户希望fb共享某些东西时我们需要publish_stream权限。

Does anyone know how to elevate the fb permissions? 有谁知道如何提升fb权限? to for example: 'email,offline_access,user_about_me,publish_stream' 举例来说:'email,offline_access,user_about_me,publish_stream'

I'm aware that the user has to go through the oauth dialog again..but how to do this? 我知道用户必须再次通过oauth对话框..但是如何做到这一点?

thanks 谢谢

First you need to add setup: true to be able to upgrade the list of permissions of the service: 首先,您需要添加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

Add two routes in routes.rb : 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

The first route is where the user should be linked to by using user_omniauth_upgrade_path(:facebook) . 第一个路径是用户应该使用user_omniauth_upgrade_path(:facebook)链接到的user_omniauth_upgrade_path(:facebook) The second setup route is the callback which omniauth will call internally and we can use to change the scope parameter. 第二个设置路径是omniauth将在内部调用的回调,我们可以使用它来更改scope参数。

These go into omniauth_callbacks_controller.rb : 这些进入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

When you specify setup: true inside of the omniauth configuration setup_path is called by default. 在omniauth配置中指定setup: true ,默认情况下会调用setup_path We will use this to change the scope from the default in the strategy. 我们将使用它来更改策略中的默认范围。 Add this to omniauth_callbacks_controller.rb : 将其添加到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

Finally, in your views you can add: 最后,在您的视图中,您可以添加:

<%= link_to "Upgrade Access", user_omniauth_upgrade_path(:facebook) %>

Source: http://willschenk.com/setting-up-devise-with-twitter-and-facebook-and-other-omniauth-schemes-without-email-addresses/#passing-dynamic-scopes-to-omniauth 资料来源: http//willschenk.com/setting-up-devise-with-twitter-and-facebook-and-other-omniauth-schemes-without-email-addresses/#passing-dynamic-scopes-to-omniauth

只使用两个权限登录Facebook - 1)电子邮件2)publish_stream

config.omniauth :facebook, ENV['FACEBOOK_KEY'], ENV['FACEBOOK_SECRET'],{:scope => 'email, publish_stream', :client_options => { :ssl => { :ca_file => "#{Rails.root}/config/ca-bundle.crt" } } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM