New Shopify Certification now available: Liquid Storefronts for Theme Developers

Ruby private app access to Shopify Resources

fwallace
Tourist
11 0 2

You want a private app, simple REST Shopify script in Ruby using the latest shopify_api gem to download resources like orders, customers, products etc. [Maybe to store in a Database, do something with them].

 

After much trial/error and reading the official docs on Rubydoc.info, here is what works for me:

 

def get_orders
        puts "Starting ..."

        ShopifyAPI::Context.setup(
            api_key: "DUMMY",
            api_secret_key: @app_token,
            scope: "DUMMY",
            host_name: "DUMMY",
            private_shop: "#{@shopname}.myshopify.com",
            #session_storage: ShopifyAPI::Auth::FileSessionStorage.new,
            #session_storage: CustomSessionStorage.new,
            is_embedded: false, 
            is_private: true, 
            api_version: "2023-07"
        
        )

        

        session = ShopifyAPI::Auth::Session.new(shop: "#{@shopname}.myshopify.com", access_token: @app_token)

        puts session.inspect

        client = ShopifyAPI::Clients::Rest::Admin.new(session: session)

        


        

        response = client.get(path: "orders", query: { created_at_min: '2023-05-01T00:59:59-08:00', created_at_max: '2023-07-18T23:59:59-08:00', status: 'any', limit: 250 })

        response.body['orders'].each do |myr|
            puts "-----------"
            puts myr.inspect
            puts "-----------"
        end

        loop do
            puts "sleeping 10 seconds ..."
            sleep 10

            break unless response.next_page_info
            response = client.get(path: "orders", query: {  limit: 250, page_info: response.next_page_info })

            response.body['orders'].each do |myr|
                puts "-----------"
                puts myr.inspect
                puts "-----------"
            end

        end



    end

 

 

Hope this helps someone.

Replies 4 (4)
fwallace
Tourist
11 0 2

Forgot to add, works with shopify_api (13.1.0), ruby 3.2.2

 

caffeinesync
Shopify Partner
7 0 1

Hello @fwallace I am trying to replicate your code, but IM getting an weird error when fetching the data: 

eval error: {"errors":"[API] Invalid API key or access token (unrecognized login or wrong password)","error_reference":"If you report this error, please include this id: 4bb181fe-6d59-4377-893a-c70bdea43fef."}

The data into  api_key: "DUMMY", api_secret_key: @app_token,

 

is the one here right? 

Screenshot 2023-07-31 at 17.36.18.png

fwallace
Tourist
11 0 2

Hi, the @app_token is the app token in setting up a private app:

 

access_token.png

 

You can find it when you add a private app in the API credentials sections of creating the app.

caffeinesync
Shopify Partner
7 0 1

Thanks FWallace. I really cant find where I can create those private apps. It doesnt appears for me on my Admin screen. 

Screenshot 2023-07-31 at 22.28.00.png