lineofflight / peddler

Amazon Selling Partner API (SP-API) in Ruby
https://lineofflight.github.io/peddler/
MIT License
307 stars 130 forks source link

Expected(200) <=> Actual(400 Bad Request) in feeds (submit feed) #39

Closed stravas closed 9 years ago

stravas commented 9 years ago

Hello there,

I've got the issue that i get a bad request error during submit_feed . The curious thing is that the same process work in version 0.9.2. Now i'm using version 0.16.0

I've used this to make a new client:

 client = MWS.feeds(
              primary_marketplace_id: 'A1F83G8C2ARO7P',
              merchant_id:  xxx,
              aws_access_key_id: xxx,
              aws_secret_access_key: xxx
              )

Then I create my latin1 string to set it in the first parameter(feed_content) of method "submit feed" and set as second paramter the feed_type:

client.submit_feed(latin_1_string, '_POST_FLAT_FILE_LISTINGS_DATA_').parse 

The trace look like this:

from /home/stravas/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/excon-0.39.6/lib/excon/middlewares/expects.rb:6:in `response_call'
    from /home/stravas/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/excon-0.39.6/lib/excon/middlewares/response_parser.rb:8:in `response_call'
    from /home/stravas/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/excon-0.39.6/lib/excon/connection.rb:363:in `response'
    from /home/stravas/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/excon-0.39.6/lib/excon/connection.rb:233:in `request'
    from (eval):3:in `post'
    from /home/stravas/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/peddler-0.16.0/lib/peddler/client.rb:173:in `run'
    from /home/stravas/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/peddler-0.16.0/lib/mws/feeds/client.rb:27:in `submit_feed'
    from (irb):16
    from /home/stravas/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/railties-4.1.1/lib/rails/commands/console.rb:90:in `start'
    from /home/stravas/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/railties-4.1.1/lib/rails/commands/console.rb:9:in `start'
    from /home/stravas/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/railties-4.1.1/lib/rails/commands/commands_tasks.rb:69:in `console'
    from /home/stravas/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/railties-4.1.1/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
    from /home/stravas/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/railties-4.1.1/lib/rails/commands.rb:17:in `<top (required)>'
    from /home/stravas/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:247:in `require'
    from /home/stravas/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:247:in `block in require'
    from /home/stravas/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:232:in `load_dependency'
    from /home/stravas/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:247:in `require'
    from /home/stravas/Documents/Projects/wandfabrik-web/bin/rails:8:in `<top (required)>'
    from /home/stravas/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:241:in `load'
    from /home/stravas/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:241:in `block in load'
    from /home/stravas/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:232:in `load_dependency'
    from /home/stravas/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:241:in `load'
    from /home/stravas/.rbenv/versions/2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /home/stravas/.rbenv/versions/2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'

This worked fine in peddler version 0.9.2 except that i used marketplace_id in client instead of primary_marketplace_id. This was the only thing i've changed after update to 0.16.0. But now i only get the bad request exception. I've also tried on different marketplaces but always with the same result.

Maybe i miss something.

Thanks for helping.

hakanensari commented 9 years ago

Odd. The only change I can think of is adding Version to the request parameters here and here.

Could you please rescue and check the response body?

begin
  client.submit_feed #some args
rescue => e
  puts e.response.body
end
stravas commented 9 years ago

ok i'm not this deep in ruby to see the mistake in the code. Here is the response:

<?xml version="1.0"?>
<ErrorResponse xmlns="http://mws.amazonaws.com/doc/2009-01-01/">
  <Error>
    <Type>Sender</Type>
    <Code>InvalidRequest</Code>
    <Message>parameter MWSAuthToken failed a validation check: value cannot be empty</Message>
    <Detail/>
  </Error>
 <RequestID>ce914bb0-b4ce-416e-9414-e914752d8e6c</RequestID>
</ErrorResponse>
hakanensari commented 9 years ago

I'm assuming the credentials belong to a seller who's using your app? You now need to provide an MWSAuthToken for them with the request.

The older Peddler version should fail as well. I have no idea why if it indeed is not.

stravas commented 9 years ago

is this what you mean?

If you are creating a client for another seller, pass the latter's MWSAuthToken to the client.

client.auth_token = "corge"
hakanensari commented 9 years ago

Yes. Is this the culprit?

stravas commented 9 years ago

what do I have to write in there instead of "corge"?

I thought I should get the auth_token from seller with client.get_auth_token but this only give me another bad request(400).

The message look like this

<?xml version=\"1.0\"?>
<ErrorResponse xmlns=\"https://mws.amazonservices.com/Sellers/2011-07-01\">
  <Error>
    <Type>Sender</Type>
    <Code>InvalidParameterValue</Code>
    <Message>Cannot request MWSAuthToken for own account</Message>
  </Error>
  <RequestId>109606f4-9f73-4c92-8afb-b1aac4b6b759</RequestId>
</ErrorResponse>

and how should the inkove look like after that? maybe like this?

client.auth_token.submit_feed(latin_1_string, '_POST_FLAT_FILE_LISTINGS_DATA_').parse
stravas commented 9 years ago

I've got it !!!

auth_thoken was the culprit. Just before you use the client you have to do this:

client.auth_token = "something" 
client.submit_feed(latin_1_string, '_POST_FLAT_FILE_LISTINGS_DATA_').parse

Thanks a lot

PS: I appreciate you work hakanensari. This code saved me very much time in the fight with Amazon MWS.

hakanensari commented 9 years ago

:+1: