4
votes

I'm trying to familiarize myself with the concept of using script tags. I'm making a ruby on rails app that does something as simple as alert "Hi" when a customer visits a page. I am testing this public app on a local server and I have the shopify_app gem installed. The app has been authenticated and I have access to the store's data. I've viewed the Shopify API documentation on using script tags and I've looked at the Shopify Embedded App example that Shopify has on GitHub. The documentation details the properties of a script tag and gives examples of script tags with their properties defined, but doesn't say anything about where to place the script tag in an application, or how to configure an environment so that the js file in the script tag will go through.

I've discovered that a js file being added with a script tag will only work if the js file is hosted online, so I've uploaded the js file to google drive. I have the code for the script tag in the index action of my HomeController (the default page for the app). This is the code I'm using:

def index
  if response = request.env['omniauth.auth']
  sess = ShopifyAPI::Session.new(params[:shop], response[:credentials][:token])
  session[:shopify] = sess

  ShopifyAPI::Base.activate_session(sess)
  ShopifyAPI::ScriptTag.create(
       :event => "onload", 
       :src => "https://drive.google.com/..."
  )
end

I think the problem may be tied to the request.env. The response is not being read as request.env[omniauth.auth] and I believe that the response coming back as valid may be required for the script tag to go through.

The method that I tried above is from the 2nd answer given in this topic: How to develop rails app for shopify with ScriptTags.

The first answer suggested using this code: ShopifyAPI::Base.site = token

s = ShopifyAPI::ScriptTag.create(:events => "onload",:src => "your javascript url")

However, it doesn't say where to place both lines of code in a rails application. I tried putting the second line in a js file in my rails application, but it did not work.

I don't know if I'm encountering problems because I'm running the app on a local server or if there is something missing from the configuration of my application.

I'd appreciate it if anyone could point me in the right direction.

1

1 Answers

3
votes

Try putting something like this in config/initializers/shopify_app.rb

ShopifyApp.configure do |config|
  config.api_key = "xxx-xxxx-xxx-xxx"
  config.secret = "xxx-xxxx-xxx-xxx"
  config.scope = "read_orders, read_products"
  config.embedded_app = true
  config.scripttags = [
  {event:'onload', src: 'https://yourdomain.herokuapp.com/javascripts/yourjs.js'}
  ]
end

Yes, you are correct that you'll need the js file you want to include for your scripttag publicly available - if you are using localhost for development look into grok.

Do yourself the favor of ensuring your callbacks use SSL when interacting with the Shopify API (i.e. configure your app with https://localhost/ as callback setting in the Shopify app settings). I went through the trouble of configuring thin as the web server locally with a self signed SSL certificate.

With a proper set up you should be able to debug why the response is failing the omniauth check.

I'm new to the Shopify API(s), but not Rails. Their documentation leaves a lot to be desired.

Good luck to you sir,