Webhook callback returning a 401 [RAILS]

joshmvandercom
Visitor
1 0 0

I have a custom app with only one shop.

My call backs and even in the console when I try to authenticate then call the API I get a 401.

 

ActiveResource::UnauthorizedAccess: Failed.  Response code = 401.  Response message = Unauthorized ([API] Invalid API key or access token (unrecognized login or wrong password))

Failing code is below

 

# Controller

class OrdersController < ApplicationController
  before_action :set_shopify_tenancy_and_api!
  include ShopifyApp::WebhookVerification

  def create
    order = ShopifyAPI::Order.find(params[:id])

    ShopifyOrderProcessor.new(order).process!

    render json: {}
  end

  private

  def set_shopify_tenancy_and_api!
    Shop.first.login_with_session!
  end
end

# shop.rb
class Shop < ActiveRecord::Base
  include ShopifyApp::ShopSessionStorage

  def login_with_session!
    session = ShopifyAPI::Session.new(domain: shopify_domain, token: shopify_token)
    ShopifyAPI::Base.activate_session(session)
  end
end

 

Reply 1 (1)

Eugene_404
Shopify Partner
5 0 1

I would check couple things here:

  1. Check that config.scope has "read_orders".
  2. When you create ShopifyAPI::Session you should pass three parameters:

 

ShopifyAPI::Session.new(domain: shopify_domain, token: shopify_token, api_version: api_version)​

 

  • Try to create and execute a test New Order webhook in your development store in Notifications.
  • Reinstall your app. Make sure that the record in shops table was deleted, and then created.

Hope one of the above will help. Good luck!