Shopify_app: How to: API Calls from console

Created on 26 Aug 2016  路  13Comments  路  Source: Shopify/shopify_app

No issue here. How to make api calls from the rails console?

ShopifyAPI::Product.find(:all)

I thought that would work. No? How?

All 13 comments

You need to set the session first since the API client doesn't know what credentials to use. I usuallu setup a method on the shop model to set itself as the active session for console use.

@kevinhughes27 A tiny bit in layman's terms?

Here is a method that I have used (on the shop model):

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

@kevinhughes27 Cool. Is there a before filter for that method? After that, what's next? I could go in console and make calls?

I call it manually in the console for the shop I am working with, eg. shop.with_shopify!

@kevinhughes27 Last question here, I now get:

ActiveResource::UnauthorizedAccess: Failed.  Response code = 401.  Response message = Unauthorized

Where to change the permission?

Is your shop connected properly to begin with? Does your home controller render products like it does after the default generation? I don't really have enough context on what you are doing to help with this error message.

Yes, connected properly. I can see my products etc. Im following this guide here. In console:

# https://app.shopify.com/services/partners/api_clients/<id>

token = # I got this from app credentials under refresh token
session = ShopifyAPI::Session.new("SHOP_NAME.myshopify.com", token)

ShopifyAPI::Base.activate_session(session)

shop = ShopifyAPI::Shop.current # ActiveResource::UnauthorizedAccess: Failed.  Response code = 401.  Response message = Unauthorized

shopify_app.rb:

  [...]
  config.scope = "read_orders, read_products, write_products, read_customers"
  config.embedded_app = true
end

ooooh okay so when you are using Shopify app all the initial token exchange is done by omniauth so don't do any of this manually. The docs in Shopify api itself are there if you are not using this framework.

So I suspect you are using the wrong token. You want the token that is saved onto your shop model by omniauth. This is the token that is used for your home controller.

Does that make sense?

(you can see the code this framework uses here https://github.com/Shopify/shopify_app/blob/master/lib/shopify_app/login_protection.rb#L9)

I dont see an easy way to use the console without a token. I have to put this in the controller:

@token = ShopifyAPI::Base.activate_session(shop_session)

Then got to view to copy token, the repeat token process as in my previous comment.

you need the token but that token is already in your database after installing the app for a shop

OHHHHHHHHHH!!!!!

Shop.first.shopify_token

Thanks!

You are welcome! Happy developing!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jakeyeaton16 picture jakeyeaton16  路  6Comments

arialblack14 picture arialblack14  路  5Comments

skillmatic-co picture skillmatic-co  路  6Comments

wlbrough picture wlbrough  路  6Comments

resistorsoftware picture resistorsoftware  路  8Comments