我正在尝试使用amazon ecs gem来访问amazon产品广告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错误请求错误。有什么我能做的吗?

最佳答案

我有一个类似的问题,我是如何解决的:
首先,我调用了Amazon::Ecs.methods并看到存在debug,并且debug=方法,Amazon::Ecs.debug被设置为false,因此自然地我将其设置为trueAmazon::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

关于ruby - Amazon ECS Gem返回400错误的请求错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17540876/

10-16 12:56