简体   繁体   English

对亚马逊 MWS API 的 Python HTTP POST 请求

[英]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.我没有太多处理 API 的经验,而且在使用 Python 发出 HTTP POST 请求方面还很陌生。 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.他们提供了这个工具来手动访问数据,但我希望使用 Python 的请求库来实现自动化。 Plugging in the necessary information, I get this as the HTTP POST:插入必要的信息,我得到这个作为 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:它还吐出一个 POST URL,我想我可以用它来提取我需要的数据,但是当我把它扔到我的浏览器中时我得到了这个:

Sender InvalidParameterValue Either Action or Operation query parameter must be present. Sender InvalidParameterValue Action 或 Operation 查询参数必须存在。

How would I go about sending this same request using Python?我将如何使用 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:不过,我收到一条带有此消息的 404:

Resource / is not found on this server.在此服务器上找不到资源 /。 API Section is missing or you have provided an invalid operation name. API 部分丢失或您提供了无效的操作名称。

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 .假设您可以使用 HTTP GET 使其工作并希望它使用 HTTP POST,则您的 URL 应该仅为/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.省略问号并将 URL 的其余部分(即AWSAccessKeyId=及其后的所有内容)放入 POST 请求的正文中。 Add a Content-Type header with value application/x-www-form-urlencoded .添加值为application/x-www-form-urlencodedContent-Type标头。

Long answer:长答案:

I just ran into the same problem (I couldn't see how exactly to get POST to work).我刚刚遇到了同样的问题(我不知道如何让 POST 正常工作)。 I'm trying to estimate fees for 20 ASINs at once using the GetMyFeesEstimate API call.我尝试使用GetMyFeesEstimate API 调用一次估算 20 个 ASIN 的费用。 With HTTP GET I was getting error 414 (request-URI too large), so I decided to try and get HTTP POST working.使用 HTTP GET 我收到错误 414(请求 URI 太大),所以我决定尝试让 HTTP POST 工作。

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. MWS 文档仅指出“查询请求是简单的 HTTP 请求,分别在 URL 或 HTTP 正文中使用带有查询参数的 GET 或 POST 方法”,但不清楚具体如何执行此操作。

I found some more details in the Amazon SQS documentation , showing how to construct a POST request with query params in the body.我在 Amazon SQS 文档中找到了更多详细信息,展示了如何在正文中构造带有查询参数的 POST 请求。 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).它希望对它们进行形式 URL 编码,但我发现只是获取我的 GET 请求的 URL 并将其放入请求正文(如上所述)对我有用(所以编码可能是兼容的,没有仔细检查) .

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 ) nonagon 的答案有效,但请注意,您必须将 POST 正文作为字符串发送,而不是字典(注意Content-Typeapplication/x-www-form-urlencoded ,而不是application/json

So your POST body should be like this:所以你的 POST 正文应该是这样的:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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