Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在运行时为omniauth-shopify-oauth2 gem切换提供程序?_Ruby On Rails_Oauth_Oauth 2.0_Shopify_Omniauth - Fatal编程技术网

Ruby on rails 在运行时为omniauth-shopify-oauth2 gem切换提供程序?

Ruby on rails 在运行时为omniauth-shopify-oauth2 gem切换提供程序?,ruby-on-rails,oauth,oauth-2.0,shopify,omniauth,Ruby On Rails,Oauth,Oauth 2.0,Shopify,Omniauth,gem的初始值设定项应该如下所示: # config/initializers/omniauth.rb Rails.application.config.middleware.use OmniAuth::Builder do provider :shopify, ENV['SHOPIFY_API_KEY'], ENV['SHOPIFY_SHARED_SECRET'] end 然而,在我们的Rails应用程序中,有几个不同的品牌提供相同的功能。在整个应用程序中,请求的request.doma

gem的初始值设定项应该如下所示:

# config/initializers/omniauth.rb
Rails.application.config.middleware.use OmniAuth::Builder do
  provider :shopify, ENV['SHOPIFY_API_KEY'], ENV['SHOPIFY_SHARED_SECRET']
end
然而,在我们的Rails应用程序中,有几个不同的品牌提供相同的功能。在整个应用程序中,请求的
request.domain
决定您接触的品牌(
brand1.example.com
brand2.example.com
,等等)

我们可以轻松存储特定于品牌的凭据,并将用户重定向到特定于品牌的授权路径:

https://example.myshopify.com/admin/oauth/authorize?client_id=brand1&scope=read_orders,read_products&redirect_uri=https://brand1.example.com/auth/shopify/callback

但是,我不知道如何根据访问的
请求.domain
为中间件选择不同的提供者。您知道如何设置它吗?

Omniauth提供了关于的文档,在这里会很有帮助。比如:

# config/initializers/omniauth.rb
Rails.application.config.middleware.use OmniAuth::Builder do
  provider :shopify, setup: lambda do |env|

    # Do logic to get correct credentials for request.
    # For example, if you store the credentials on a model called Brand,
    # and have it keyed on "subdomain":
    request = ActionDispatch::Request.new(env)
    brand = Brand.find_by(subdomain: request.subdomain)

    env['omniauth.strategy'].options.merge!({
      client_id: brand.client_id,
      client_secret: brand.client_secret
    })

    # `site` needs to be set. This is part of the shopify provider setup phase, which we are overriding
    env['omniauth.strategy'].options[:client_options][:site] = "https://#{ request.GET['shop'] }"
  end
end

感谢您让我关注动态提供者。但我想我需要更彻底的演练。我应该将上述代码放在哪里?如何在会话基础上(例如在控制器中)处理这些代码而不全局更改提供程序?@JohnSmith1976:此代码放在初始值设定项中,并替换您在问题中提供的
提供程序
片段。然后,获取品牌特定值的逻辑位于lambda中,而不是任何控制器中(尽管您可能希望将此逻辑提取到PORO中,以便更好地组织代码和简化测试)。我已经更新了这个例子,希望能让它更清晰。谢谢@gwcodes,我已经测试过了,看起来很有希望。然而,我并没有得到所有的方式,因为一个例外是在。似乎
options[:client\u options][:site]
为零。似乎可以解决这个问题,但我不确定?@JohnSmith1976:尝试将
站点:“https://{request.GET['shop']}”
添加到哈希中(例如在
客户机密下
)完美,这将我引向了正确的方向。我添加了以下行:
params=Rack::Utils.parse_query(env['query\u STRING'])
env['omniauth.strategy'].options[:client_options][:site]=“https://{params['shop']}”
。然后它成功了。如果您同意这一点,请相应地更新答案,以便其他人能够使用它,然后我将确保检查答案并释放赏金。再次感谢你的帮助!