FROM CACHE - en_header

Starting a session outside of an application controller

Colin_Brogan
Shopify Partner
4 0 0

Hi,

I have a background job in my app which has been running every night for over a year. Some time after upgrading to Shopify_app 8.3.1, I run into periodic problems starting a session manually in that background job. 

 

Obviously, since a job does not inherit from the Authenticated Controller, I have to start a shopify session manually. I do this by first retrieving the last stored token in my Shop database (connected to the current domain). I then pass that token into a new session, and activate it.

 

@mazer_shop = Shop.find_by(shopify_domain: "mazer-3.myshopify.com")
extra = { "scope" => ShopifyApp.configuration.scope.split(", ") }
@shopify_session = ShopifyAPI::Session.new(@mazer_shop.shopify_domain, @mazer_shop.shopify_token, extra)
ShopifyAPI::Base.activate_session(@shopify_session)

Last month (after the upgrade),  for 29 days it activated fine and did it's work without problems. However, starting on the 30th around 11AM, it began to give this error response (the second attempt that day):

 

I, [2019-03-30T08:57:18.949278 #30482]  INFO -- : GET https://mazer-3.myshopify.com:443/admin/shop.json
I, [2019-03-30T08:57:18.949392 #30482]  INFO -- : --> 200 OK 1482 (191.1ms)
I, [2019-03-30T08:57:18.949546 #30482]  INFO -- : Headers: {"Accept"=>"application/json", "User-Agent"=>"ShopifyAPI/5.2.4 ActiveResource/5.1.0 Ruby/2.4.5", "X-Shopify-Access-Token"=>"XXXXXXOmittedHereXXXXXXX"}
I, [2019-03-30T08:57:18.949581 #30482]  INFO -- : Response:
{"shop":{"id":5302909,"name":"Mazer Appliance","email":"mazer@mazer.com","domain":"mazer.com","province":"Alabama","country":"US","address1":"2  41st Street South","zip":"35222","city":"Birmingham","source":"zeekee","phone":"2052249600","latitude":33.5260509,"longitude":-86.7758489,"primary_locale":"en","address2":"","created_at":"2014-06-08T10:59:04-05:00","updated_at":"2018-12-31T11:28:19-06:00","country_code":"US","country_name":"United States","currency":"USD","customer_email":"mazer@mazer.com","timezone":"(GMT-06:00) Central Time (US \u0026 Canada)","iana_timezone":"America\/Chicago","shop_owner":"Mike Mazer","money_format":"$ {{amount}}","money_with_currency_format":"$ {{amount}} USD","weight_unit":"lb","province_code":"AL","taxes_included":false,"tax_shipping":null,"county_taxes":true,"plan_display_name":"Shopify Plus","plan_name":"shopify_plus","has_discounts":true,"has_gift_cards":true,"myshopify_domain":"mazer-3.myshopify.com","google_apps_domain":null,"google_apps_login_enabled":null,"money_in_emails_format":"${{amount}}","money_with_currency_in_emails_format":"${{amount}} USD","eligible_for_payments":true,"requires_extra_payments_agreement":false,"password_enabled":false,"has_storefront":true,"eligible_for_card_reader_giveaway":false,"finances":true,"primary_location_id":41435139,"checkout_api_supported":true,"multi_location_enabled":true,"setup_required":false,"force_ssl":true,"pre_launch_enabled":false,"enabled_presentment_currencies":["USD"]}}
E, [2019-03-30T11:00:07.254947 #30482] ERROR -- : GET https://mazer-3.myshopify.com:443/admin/shop.json
E, [2019-03-30T11:00:07.255065 #30482] ERROR -- : --> 401 Unauthorized 89 (105.2ms)
I, [2019-03-30T11:00:07.255252 #30482]  INFO -- : Headers: {"Accept"=>"application/json", "User-Agent"=>"ShopifyAPI/5.2.4 ActiveResource/5.1.0 Ruby/2.4.5", "X-Shopify-Access-Token"=>"XXXXXXOmittedHereXXXXXXX"}
I, [2019-03-30T11:00:07.255305 #30482]  INFO -- : Response: {"errors":"[API] Invalid API key or access token (unrecognized login or wrong password)"}

The strange thing is that the first response (which ran at 8:57AM), which started the session successfully, is using the same token as the second response, which gave the invalid API key or access token response about 2 hours later. I am guessing the token expired. However, I need a way to run a background job regardless of whether or not someone has recently logged into the app and regenerated the token. I never had this problem until after upgrading. Does any one know a way?

 

Reply 1 (1)
Josh
Shopify Staff
Shopify Staff
1134 84 226

For others who may stumble upon this in the future, there is a relevant discussion about this occurring in the GitHub repository for our shopify_api gem : https://github.com/Shopify/shopify_api/issues/553

Josh | Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit the Shopify Help Center or the Shopify Blog