我将我的应用程序设置为接收 Shopify 网络钩子。我按照这里的指南进行操作
我用
include ShopifyApp::WebhookVerification
对网络钩子和我的应用进行身份验证
我设置了我的 Shopify_app.rb 文件以将 webhook 发送到正确的路由,如下所示
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'}
]
我收到网络钩子,但我不断收到消息
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'}
]
没有在安装应用程序的商店上生成网络钩子。. 还不知道为什么
我最终只是运行自动生成电子书的作业,如下所示:
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