Shopify Webhook Verification返回'过滤器链停止为:verify_request呈现或重定向'

时间:2018-01-30 16:02:50

标签: ruby-on-rails shopify shopify-app

我设置我的应用程序以接收Shopify webhooks。我按照指南here

进行了操作

我使用

创建了自己的控制器
  include ShopifyApp::WebhookVerification

验证webhooks和我的应用

我设置了Shopify_app.rb文件,将webhooks发送到正确的路线

    config.webhooks = [
    {topic: 'customers/create', address: 'https://*******.ngrok.io/webhooks/new_contact'}, 
    {topic: 'checkouts/update', address: 'https://*******.ngrok.io/webhooks/checkout_update'},
    {topic: 'orders/create', address: 'https://*******.ngrok.io/webhooks/orders_create'}
  ]

我收到了Webhooks,但我一直收到消息

Filter chain halted as :verify_request rendered or redirected

这是我的控制器:

   class WebhooksController < ApplicationController
      include ShopifyApp::WebhookVerification


      #webhook that handles new contacts in the shopify application
      def new_contact
        shop = ShopifyShop.getShop(shop_domain)

        if !shop
          render json: {success: true} and return
        end

          # begin
            raw_post = request.raw_post.force_encoding("UTF-8")

            contact.newShopifyContact(shop.default_tags, shop.organization_id,raw_post)
          # rescue Exception => e 
          #      ExceptionLog.track(e.class.name, e.message, e.backtrace) 
          # end        
        render json: {success: true} 
      end
  end
接收请求时

终端输出:

https://pastebin.com/gkXCiFa4

我已经尝试添加

   skip_before_action :verify_authenticity_token

因为我认为我的应用认证可能导致401

当我删除include ShopifyApp::WebhookVerification时,它会进入我的方法new_contact,所以我认为问题出在include ShopifyApp::WebhookVerification

我尝试使用此代码自行验证请求中的HMAC:

      header_hmac = request.headers["HTTP_X_SHOPIFY_HMAC_SHA256"]
  digest = OpenSSL::Digest.new("sha256")
  request.body.rewind
  calculated_hmac = Base64.encode64(OpenSSL::HMAC.digest(digest, ENV['SHOPIFY_SECRET_KEY'], request.body.read)).strip

  puts "header hmac: #{header_hmac}"
  puts "calculated hmac: #{calculated_hmac}"

  puts "Verified:#{ActiveSupport::SecurityUtils.secure_compare(calculated_hmac, header_hmac)}"

验证结果是错误的,我正在为我的应用程序使用正确的API私钥我不确定是否可能是我需要的第三个密钥?

更新1: 似乎问题是

  config.webhooks = [
    {topic: 'customers/create', address: 'https://{url}.ngrok.io/shopify/app/customers_create'}, 
    {topic: 'checkouts/update', address: 'https://{url}.ngrok.io/shopify/app/checkouts_update'},
    {topic: 'orders/create', address: 'https://{url}.ngrok.io/shopify/app/orders_create'}
  ]

没有在正在安装应用的商店中生成webhook ..不确定原因

1 个答案:

答案 0 :(得分:0)

我最终只是自动运行生成webooks的作业:

    webhooks = [
        {topic: 'customers/create', address: 'https://85bcff59.ngrok.io/shopify_webhooks/new_contact'}, 
        {topic: 'checkouts/update', address: 'https://85bcff59.ngrok.io/shopify_webhooks/checkouts_update'},
        {topic: 'orders/create', address: 'https://85bcff59.ngrok.io/shopify_webhooks/orders_create'}
    ]

    ShopifyApp::WebhooksManagerJob.perform_now(shop_domain: shopify_domain, shop_token: shopify_token, webhooks: webhooks)

您可以在此处找到要运行的作业的代码:https://github.com/Shopify/shopify_app/blob/master/lib/shopify_app/jobs/webhooks_manager_job.rb#L8