【问题标题】:Shopify API - Response code = 429. Response message = Too Many RequestsShopify API - 响应代码 = 429。响应消息 = 请求过多
【发布时间】:2017-05-16 22:22:25
【问题描述】:

我正在使用带有 Rails 的 Shopify API。但我收到一个错误。 我不知道为什么会出现这个错误。

ActiveResource::ClientError: Failed.  Response code = 429.  Response message = Too Many Requests.

我已遵循本教程:https://help.shopify.com/api/getting-started/api-call-limit。这是我的代码:

cycle = 0.5

product_count = ShopifyAPI::Product.count
nb_pages      = (product_count / 250.0).ceil

start_time = Time.now
1.upto(nb_pages) do |page|
    unless page == 1
        stop_time = Time.now
        processing_duration = stop_time - start_time
        wait_time = (cycle - processing_duration).ceil
        sleep wait_time if wait_time > 0
        start_time = Time.now
    end


    if product_list == 'all'
        products = ShopifyAPI::Product.find( :all, :params => { :limit => 250, :page => page } )
    elsif product_list.include? 'category'
        product_list.slice! "category"
        products = ShopifyAPI::Product.where(:collection_id => product_list, :limit => 250, :page => page )
    else
        products = ShopifyAPI::Product.find(:all, params: {ids: product_list})
    end

    products.each do |product|
        variants = Array.new
        metafields = Array.new
        product.variants.each do |variant|
            variants << variant
            metafields << product.metafields
            if variant.title.include? "+"
                next
            end
            quantity.each_with_index  do |q, i|

                unless customer_tag.to_s.strip.empty?
                    variants << {
                                    "title"=>"c #{variant.title} #{q}+",
                                    "price"=> price_calculate(calculation_type, discount_value[i], variant.price),
                                    "inventory_policy"=>"deny",
                                    "option1"=>"c #{variant.option1} #{q}+",
                                    "option2"=>nil,
                                    "option3"=>nil,
                                    "created_at"=> Time.now,
                                    "updated_at"=> Time.now,
                                    "taxable"=> variant.taxable,
                                    "inventory_quantity"=>1,
                                    "weight"=> variant.weight,
                                    "weight_unit"=> "#{variant.weight_unit}",
                                    "requires_shipping"=> variant.requires_shipping
                                }
                else
                    variants << {
                                    "title"=>"#{variant.title} #{q}+",
                                    "price"=> price_calculate(calculation_type, discount_value[i], variant.price),
                                    "inventory_policy"=>"deny",
                                    "option1"=>"#{variant.option1} #{q}+",
                                    "option2"=>nil,
                                    "option3"=>nil,
                                    "created_at"=> Time.now,
                                    "updated_at"=> Time.now,
                                    "taxable"=> variant.taxable,
                                    "inventory_quantity"=>1,
                                    "weight"=> variant.weight,
                                    "weight_unit"=> "#{variant.weight_unit}",
                                    "requires_shipping"=> variant.requires_shipping
                                }
                end

            end

        end

        unless customer_tag.to_s.strip.empty?
            metafields << {
                            "namespace": "customer_tag",
                            "key": "#{customer_tag}",
                            "value": "#{customer_tag}",
                            "value_type": "string"
                        }
        end

        product.variants = variants
        product.metafields = metafields
        product.save
    end
end

为什么会显示 Shopify API 限制错误?按照他们的教程,我只进行了 1 个 API 调用。请任何想法..

【问题讨论】:

  • 您是否按照链接页面上的说明使用 APIKEY、PASSWORD 和 SHOPNAME 创建了cleanup.rb
  • 我使用了这个宝石github.com/Shopify/shopify_app。其他人工作完美。
  • 您使用的是自己的 API 密钥吗?见shopify_app - Managing Api Keys
  • 是的。我已正确配置此应用
  • 好吧,那么我只有 2 条建议:1. 确保您的应用没有进行多次调用,2. 您可能应该联系 Shopify 以解决您的 API 访问问题

标签: ruby-on-rails ruby-on-rails-5 shopify


【解决方案1】:

Shopify 遵循漏桶算法,这意味着它允许不频繁的调用突发。但是您需要平均 0.5 秒/呼叫。这些是对应于脚本中不同 API 调用的方法:

  1. ShopifyAPI::Product.find
  2. product.metafields
  3. product.save

使用您上面编写的时序管理器仅管理第一个调用。接下来的两个电话也需要参与其中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多