简体   繁体   中英

Python HTTP POST Request to Amazon MWS API

I don't have much experience dealing with APIs, and am fairly new at making HTTP POST requests with Python. Wondering if someone could give me hand with this.

They provide this tool to access the data manually, but I'm hoping to use Python's requests library to automate things. Plugging in the necessary information, I get this as the HTTP POST:

POST /Products/2011-10-01?AWSAccessKeyId=AKIAJEHA562QWNAIEIKA
  &Action=GetLowestOfferListingsForASIN
  &SellerId=<removed>
  &MWSAuthToken=<removed>
  &SignatureVersion=2
  &Timestamp=2016-01-06T04%3A20%3A48Z
  &Version=2011-10-01
  &Signature=7vP5dwgpk%2Bi%2Bl1L9tOsd7tVXDeQOoyvjR2fYtEhJCEY%3D
  &SignatureMethod=HmacSHA256
  &MarketplaceId=<removed>
  &ASINList.ASIN.1=B00GQLLCTA HTTP/1.1
Host: mws.amazonservices.com
x-amazon-user-agent: AmazonJavascriptScratchpad/1.0 (Language=Javascript)
Content-Type: text/xml

It also spits out a POST URL, which I figured I'd be able to use to pull the data I need, but I get this when I throw it into my browser:

Sender InvalidParameterValue Either Action or Operation query parameter must be present.

How would I go about sending this same request using Python?

Any help would be greatly appreciated!

Thanks

EDIT:

So far i've tried passing the following dictionary containing all of the parameters shown in the scratchpad tool, with the post request:

params = {
    'AWSAccessKeyId':'<removed>',
    'Action':'GetMyPriceForASIN',
    'SellerId':'<removed>',
    'MWSAuthToken':'amzn.mws.fca4aee8-689d-74a2-5430-128d34f56873',
    'SignatureVersion':'2',
    'Timestamp':'2016-01-06T04%3A09%3A24Z',
    'Version': '2011-10-01',
    'Signature':'TZX1AP3dwl%2Fy3y5RLjZGorZFVLtQvW40KJ3IrTmWrw0%3D',
    'SignatureMethod':'HmacSHA256',
    'MarketplaceId':'<removed>',
    'ASINList.ASIN.1':'B004KZQVF4'
    }

page = requests.post("https://mws.amazonservices.com", params=params)

I get a 404 with this message though:

Resource / is not found on this server. API Section is missing or you have provided an invalid operation name.

Short answer:

Assuming you can get this working with HTTP GET and want it to work with HTTP POST, your URL should be only /Products/2011-10-01 . Omit the question mark and put the rest of the URL (ie AWSAccessKeyId= and everything after it) in the body of the POST request. Add a Content-Type header with value application/x-www-form-urlencoded .

Long answer:

I just ran into the same problem (I couldn't see how exactly to get POST to work). I'm trying to estimate fees for 20 ASINs at once using the GetMyFeesEstimate API call. With HTTP GET I was getting error 414 (request-URI too large), so I decided to try and get HTTP POST working.

The MWS documentation states only "Query requests are simple HTTP requests, using the GET or POST method with query parameters in the URL or HTTP body, respectively", but it wasn't clear exactly how to do this.

I found some more details in the Amazon SQS documentation , showing how to construct a POST request with query params in the body. It wants them form-URL-encoded, but I found just taking the URL for my GET request and putting it in the request body (as described above) worked for me (so perhaps the encodings are compatible, didn't check this carefully).

The answer by nonagon works, but please note that you have to send POST body as string, instead of dictionary (notice the Content-Type is application/x-www-form-urlencoded , not application/json )

So your POST body should be like this:

AWSAccessKeyId=AKIAJEHA562QWNAIEIKA
&Action=GetLowestOfferListingsForASIN
&SellerId=<removed>
&MWSAuthToken=<removed>
&SignatureVersion=2
&Timestamp=2016-01-06T04%3A20%3A48Z
&Version=2011-10-01
&Signature=7vP5dwgpk%2Bi%2Bl1L9tOsd7tVXDeQOoyvjR2fYtEhJCEY%3D
&SignatureMethod=HmacSHA256
&MarketplaceId=<removed>
&ASINList.ASIN.1=B00GQLLCTA

Full python code:

url = 'https://mws.amazonservices.jp/Products/2011-10-01'
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
data = <The big string above>

res = requests.post(url, data=body, headers=headers)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM