InvalidParameterValue error returned when calling the Amazon MWS API

Asked 2 years ago, Updated 2 years ago, 21 views

You are trying to create a program using Amazon MWS.
I'm looking at this specification and implementing it, but something is missing.

http://docs.developer.amazonservices.com/ja_JP/dev_guide/DG_ClientLibraries.html

#-*-coding:utf-8-*-
from time import strftime, gmtime
from requests import request
import base64
import hashlib
import hmac
import urllib.parse

param = {
    'AWSAcessKeyId': 'xxxx',
    'Action': 'ListMatching Products',
    'SellerId': 'xxxx',
    'SignatureMethod': 'HmacSHA256',
    'SignatureVersion': '2',
    'Timestamp': strftime("%Y-%m-%dT%H:%M:%SZ", gmtime()) ,
    'Version': '2011-10-01',
    'Query': "harrypotter"
}

data="POST\nmws.amazonservices.jp\nProducts/2011-10-01\n/"
format_param="&".join(['{0}={1}.format(key, urllib.parse.quote(param[key], safe='-_.~').encode('utf-8'))) for key in sorted(param)])
before_sign_data="{0}{1}.format(data,format_param)
hmacstr=hmac.new(b"secretKey", bytes(before_sign_data.encode('UTF-8')), hashlib.sha256).digest()
base64str = base64.b64encode(hmacstr)

url="{0}{1}?{2}&Signature={3}".format("https://mws.amazonservices.jp", "/Products/2011-10-01", format_param, urlib.parse.quote(base64str))
headers={'User-Agent':'python-app-mws/0.0.1(Language=Python)'}
response=request("POST", url_param, data={}, headers=headers)
data=response.content

print(data)

The runtime errors are as follows:

b'<?xml version="1.0"?>\n;ErrorResponse xmlns="https://mws.amazonservices.com/">\n  <Error>\n    <Type>Sender</Type>\n    <Code>InvalidParameterValue</Code>\n    <Message>Value b&apos;2&apos; for parameter SignatureVersion is invalid.</Message>\n  </Error>\n  <RequestID>6e29a554-c2d2-4b23-9f99-276aa250d239</RequestID>\n</ErrorResponse>\n'

When I sent it, the URL was as follows:

https://mws.amazonservices.jp/Products/2011-10-01?AWSAccessKeyId=b'xxxxx'&Action=b'ListMatchingProducts'&Query=b'harry%20potter'&SellerId=b'xxxxx'&SignatureMethod=b'HmacSHA256'&SignatureVersion=b'2'&Timestamp=b'2017-03-07T15%3A22%3A32Z'&Version=b'2011-10-01'&Signature=v/lujNiPl8NZTkWmDQXRsAfhiX1GTLOOE7erK26QHDA%3D

If you notice anything, please let me know.

python amazon-mws

2022-09-30 19:31

2 Answers

Value b'2' for parameter SignatureVersion is invalid.

The SignatureVersion value "b'2'" is incorrect.

Maybe it's because the GET parameter value in the URL is "b'...'".
&SignatureVersion=2 will change.

Compare it to the example in Create URL in the referenced document.


2022-09-30 19:31

If you remove .encode('utf-8') while creating format_param, it looks like it will work.

format_param="&".join(['{0}={1}.format(key,urlib.parse.quote(param[key],safe='-_.~')for key in sorted(param)])

I'm not familiar with python, but is there something wrong with this?


2022-09-30 19:31

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.