简体   繁体   English

使用Omniauth gem初始化后更改Twitter提供程序

[英]Change Twitter provider after initialization with Omniauth gem

I'm using Omniauth to authenticate at Twitter, but I have 2 apps registred with diferent names that I want to use depeding on the current locale(session scope). 我正在使用Omniauth在Twitter上进行身份验证,但是我有2个应用程序注册了不同的名称,我想在当前语言环境(会话范围)上使用depeding。

So I need to change the provider key and secret defined at omniauth.rb file right before user calls auth/twitter( I was thinking to do a before_filter but auth/twitter is an external link to twitter and not a regular action) or a way to config Omniauth to define providers by locale instead of define for the entire application scope. 因此,我需要在用户调用auth / twitter之前更改在omniauth.rb文件中定义的提供者密钥和机密(我当时想做一个before_filter,但auth / twitter是Twitter的外部链接,而不是常规操作)或一种方式配置Omniauth以通过语言环境定义提供程序,而不是为整个应用程序范围进行定义。

So how can I do that ? 那我该怎么办呢? Any idea? 任何想法?

What you need to do is set setup to true in omniauth builder 您需要做的是在omniauth builder中将setup设置为true

  Rails.application.config.middleware.use OmniAuth::Builder do
    provider :twitter , :setup => true
  end

Then set add following to your routes.rb file to define which route will be called for setup 然后在您的routes.rb文件中设置添加以下内容,以定义将调用哪个路由进行设置

  get '/people/auth/twitter/setup' => 'sessions#twitter_setup' #needed for devise setup phase hook to work

After so just set the omniauth strategy in the session controller that meet the route for setup 之后,只需在会话控制器中设置符合设置路由的omniauth策略即可

def twitter_setup
  request.env['omniauth.strategy'].options[:consumer_key] = YOUR_DYNAMIC_KEY
    request.env['omniauth.strategy'].options[:consumer_secret] = YOUR_DYNAMIC_SECRET_KEY
    render :plain => "Setup complete.", :status => 404
end

this will enable you to load the apps you need 这将使您能够加载所需的应用程序

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

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