Amazon-ecs gem返回400 Bad Request Error

时间:2013-07-09 05:50:00

标签: ruby amazon

我正在尝试使用amazon-ecs gem访问Amazon Product Advertising API。

是的,现在我正在运行一个非常简单的脚本来运行它:

Amazon::Ecs.options = {
  :AWS_access_key_id => 'my_access_key_id',
  :AWS_secret_key => 'my_secret_key',
  :associate_tag => 'my_associate_tag'
}
res = Amazon::Ecs.item_search('ruby', :search_index => 'All')

但是,这会返回400 Bad Request错误。关于我能做什么的任何想法?

1 个答案:

答案 0 :(得分:1)

我有类似的问题,这是我如何解决它:

首先,我致电Amazon::Ecs.methods,看到有debug个,debug=方法,Amazon::Ecs.debug设置为false,所以很自然地我将其设置为true Amazon::Ecs.debug = true,然后尝试了相同的搜索:

Amazon::Ecs.item_search('ruby')

# this time it gave the following output (secret keys masked by me)

irb(main):020:0> Amazon::Ecs.item_search('ruby')
Adding AWSAccessKeyId=*****************
Adding AssociateTag=*****************
Adding Keywords=ruby
Adding Operation=ItemSearch
Adding SearchIndex=Books
Adding Service=AWSECommerceService
Adding Timestamp=2015-11-13T20:25:04Z
Adding Version=2011-08-01
Request URL: http://ecs.amazonaws.com/onca/xml?AWSAccessKeyId=*****************&AssociateTag=*****************&Keywords=ruby&Operation=ItemSearch&SearchIndex=Books&Service=AWSECommerceService&Timestamp=2015-11-13T20%3A25%3A04Z&Version=2011-08-01&Signature=****************************%3D
Amazon::RequestError: HTTP Response: 400 Bad Request
    from /Users/`whoami`/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/amazon-ecs-2.3.1/lib/amazon/ecs.rb:132:in `send_request'
    from /Users/`whoami`/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/amazon-ecs-2.3.1/lib/amazon/ecs.rb:101:in `item_search'
    from (irb):20
    from /Users/`whoami`/.rbenv/versions/2.2.2/bin/irb:11:in `<main>'

然后,我将URL复制/粘贴到浏览器中,该浏览器提供了如下的XML响应:

<ItemLookupErrorResponse xmlns="http://ecs.amazonaws.com/doc/2011-08-01/">
  <Error>
    <Code>AWS.InvalidAccount</Code>
    <Message>
        Your AccessKey Id is not registered for Product Advertising API. Please use the AccessKey Id obtained after registering at https://affiliate-program.amazon.com/gp/flex/advertising/api/sign-in.html
    </Message>
  </Error>
  <RequestId>********-****-****-****-************</RequestId>
</ItemLookupErrorResponse>

在我的特殊情况下,事实证明,我已经正确设置了所有内容,但是我刚刚创建了一个新的访问密钥,而且似乎需要几分钟才能接受我的新凭据。有趣的是,一旦我意识到可能是这种情况,并再次尝试,它就有效了!

编辑:如果不明显,答案就在邮件中: “您的AccessKey ID未在产品广告API中注册。请使用在 https://affiliate-program.amazon.com/gp/flex/advertising/api/sign-in.html注册后获得的AccessKey ID”