【问题标题】:How to get the permanent token for Shopify (Rails)?如何获得 Shopify (Rails) 的永久令牌?
【发布时间】:2014-05-22 14:39:52
【问题描述】:

我正在尝试对我的新 Shopify 应用进行身份验证。首先,我的authenticate 方法将店主重定向到 Shopify 的认证页面:

def authenticate
  ShopifyAPI::Session.setup({:api_key => "123", :secret => "456"})
  session = ShopifyAPI::Session.new("someshop.myshopify.com")
  redirect_to session.create_permission_url(["read_orders"], "https://myapp.com/shopify/post_authenticate?user=someshop")
end

一旦店主批准了集成,重定向 uri 就会触发我的post_authenticate 方法:

def post_authenticate
  ShopifyAPI::Session.setup({:api_key => "123", :secret => "456"})
  session = ShopifyAPI::Session.new("#{params[:user]}.myshopify.com")
  token = session.request_token(:code => params[:code], :signature => params[:signature], :timestamp => params[:timestamp])
end

但是request_token方法返回如下错误:

#<ShopifyAPI::ValidationException: Invalid Signature: Possible malicious login>

我在某处读到,在执行所有这些操作时,您需要在同一个 ShopifyAPI 会话中,但在 documentation 中没有这样说。而example app 采用了与文档截然不同的方法。

【问题讨论】:

  • 您是否使用旧方法进行授权?我在我的应用中使用了omniauth
  • 嗨@agmcleod。我认为授权是shopify_api gem 的一部分。我没有安装任何额外的身份验证 gem。
  • 是的,他们曾经有一种较旧的方法,该方法已被弃用以支持使用 OAuth。 github.com/Shopify/omniauth-shopify-oauth2 是我使用的宝石。我将发布我如何使用它的答案。
  • 精氨酸。所以我花了一整天的时间调试一个旧的 gem。非常好。但是感谢您发布有关如何使用它的帖子。非常感谢!
  • shopify_api gem 允许您发出 api 请求,因此它远非无用。只需要代表店铺提出请求,您需要先让店主认证。

标签: ruby-on-rails ruby ruby-on-rails-4 oauth-2.0 shopify


【解决方案1】:

根据我的评论,我使用omniauth 方法进行身份验证。这是代码的要点以供参考。 https://gist.github.com/agmcleod/7106363317ebd082d3df。把所有的sn-ps放在下面。

class ApplicationController < ActionController::Base
  protect_from_forgery
  force_ssl
  helper_method :current_shop, :shopify_session

protected

  def current_shop
    @current_shop ||= Shop.find(session[:shop_id]) if session[:shop_id].present?
  end

  def shopify_session
    if current_shop.nil?
      redirect_to new_login_url
    else
      begin
        session = ShopifyAPI::Session.new(current_shop.url, current_shop.token)
        ShopifyAPI::Base.activate_session(session)
        yield
      ensure
        ShopifyAPI::Base.clear_session
      end
    end
  end
end

在我的登录控制器中:

def create
  omniauth = request.env['omniauth.auth']
  if omniauth && omniauth[:provider] && omniauth[:provider] == "shopify"
    shop = Shop.find_or_create_by_url(params[:shop].gsub(/https?\:\/\//, ""))
    shop.update_attribute(:token, omniauth['credentials'].token)
    shopify_session = ShopifyAPI::Session.new(shop.url, shop.token)
    session[:shop_id] = shop.to_param
    redirect_to root_url
  else
    flash[:error] = "Something went wrong"
    redirect_to root_url
  end
end

config/initializers/omniauth.rb

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :shopify, Settings.api_key, Settings.api_secret,
    scope: 'write_products,write_script_tags,read_orders',
    setup: lambda { |env| params = Rack::Utils.parse_query(env['QUERY_STRING'])
                        env['omniauth.strategy'].options[:client_options][:site] = "http://#{params['shop']}" }
end

然后在您的路由文件中,适当地映射会话的创建操作:

match '/auth/shopify/callback', :to => 'login#create'

从那里我使用 shopify_session 方法作为适当控制器上的环绕过滤器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-05
    • 2023-03-30
    • 2022-10-30
    • 2013-02-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多