简体   繁体   English

所有报告请求的 SP-API“无效输入”错误

[英]SP-API "Invalid Input" error for all report requests

When I make requests to the 'Inventory' or 'Sales' endpoints, I get successful responses.当我向“库存”或“销售”端点发出请求时,我得到了成功的响应。 This confirms that my signature and IAM are correct.这证实了我的签名和 IAM 是正确的。 I am manually signing the requests because I am not sure how to use boto3 to create the signature.我正在手动签署请求,因为我不确定如何使用 boto3 创建签名。

I am not sure why, but when making POST requests, the response will state The Canonical String for this request should have been... and provides the payload_hash below.我不知道为什么,但是在发出 POST 请求时,响应将 state The Canonical String for this request should have been...并提供下面的payload_hash I am copy/pasting the string provides.我正在复制/粘贴提供的字符串。 Once I do this, I then get the InvalidInput response.一旦我这样做了,我就会得到InvalidInput响应。 However, when using GET requests, I am able to use payload_hash = hashlib.sha256(('').encode('utf-8')).hexdigest() as seen below and it works fine.但是,当使用 GET 请求时,我可以使用payload_hash = hashlib.sha256(('').encode('utf-8')).hexdigest()如下所示,它工作正常。

This is the same response as with GET_SALES_AND_TRAFFIC_REPORT and other reports.这与GET_SALES_AND_TRAFFIC_REPORT和其他报告的响应相同。

CREATING CANONICAL STRING:创建规范字符串:

t = datetime.datetime.utcnow()
amzdate = t.strftime('%Y%m%dT%H%M%SZ')
datestamp = t.strftime('%Y%m%d')

canonical_querystring = '/reports/2021-06-30/reports'
signed_headers = 'host;user-agent;x-amz-access-token;x-amz-date'
payload_hash = 'deda182f2e780e6c5e6abb9b19a087a8db6c620c39e784bf4a3384e76d742278'
# payload_hash = hashlib.sha256(('').encode('utf-8')).hexdigest()

canonical_request = method + '\n' + canonical_querystring + '\n' + '\n' +  'host:' + host + '\n' + 'user-agent:python-requests/2.27.1'  + '\n' + 'x-amz-access-token:' + access  + '\n' + 'x-amz-date:' + amzdate + '\n' + '\n' + signed_headers + '\n' +  payload_hash

REQUEST:要求:

headers = {
    'x-amz-access-token': access,
    'x-amz-date':amzdate,
    'Authorization':authorization_header,
    'Content-Type': 'application/json'
}

data = {
    'marketplaceIds':'ATVPDKIKX0DER',
    'reportType': 'GET_SALES_AND_TRAFFIC_REPORT',
    'dataStartTime':'2022-03-10T20:11:24.000Z',
    'dataEndTime':'2022-03-20T20:11:24.000Z',
    'reportOptions':{
        'dateGranularity':'WEEK',
        'asinGranularity':'CHILD'
    }
}

r = requests.post(
    'https://sellingpartnerapi-na.amazon.com/reports/2021-06-30/reports',
    headers = headers,
    data = data
)
print(r)
print(r.text)

RESPONSE:回复:

<Response [400]>
{
  "errors": [
    {
      "code": "InvalidInput",
      "message": "Invalid Input",
      "details": ""
    }
  ]
}

Took me awhile to get the signing stuff to work in postman, just looking at your request I believe MarketplaceIds needs to be an array, although you would get a specific error if that was the main problem:我花了一段时间才让签名的东西在邮递员中工作,只看你的请求,我相信 MarketplaceIds 需要是一个数组,但如果这是主要问题,你会得到一个特定的错误:

"marketplaceIds": [
    "ATVPDKIKX0DER"
]

I was just working on this.我只是在做这个。 So, I used AWS4Auth for creating a canonical string just to be sure that I'm not messing with any of it.因此,我使用 AWS4Auth 创建了一个规范字符串,以确保我没有弄乱它。

A way to do that: (Assuming you have already generated LWA access token, which I can see in your header as 'access')一种方法:(假设您已经生成了 LWA 访问令牌,我可以在您的 header 中将其视为“访问”)

import json
import boto3
from requests_aws4auth.aws4auth import AWS4Auth

client = boto3.client('sts')
aws_account_id = 'YOUR_ACCOUNT_ID'
iamrole = 'arn:aws:iam::'+aws_account_id+':role/YOU_ROLE_NAME'
response = client.assume_role(
    RoleArn= iamrole,
    RoleSessionName='SPAPIRoleSession'
)

# Initializing AccessKey, SecretKey and SessionToken variables to be used in signature signing.
AccessKeyId = response['Credentials']['AccessKeyId']
SecretAccessKey = response['Credentials']['SecretAccessKey']
SessionToken = response['Credentials']['SessionToken']

#add your region accordingly: my region is us-east-1
auth = AWS4Auth(AccessKeyId, SecretAccessKey, 'us-east-1', 'execute-api', session_token=SessionToken) 

# Create headers to add to SP API request. Headers should include: content_type and "x-amz-access-token"(b)
headers = {'content-type': 'application/json','Accept': 'application/json','x-amz-access-token':access}

#your params: I made a small change to 'dataEndTime', if you mentioned the granularity as a week, make sure the start and end dates are 7 days apart. Look at the AWS documentation for more details
data = {
'marketplaceIds':'ATVPDKIKX0DER',
'reportType': 'GET_SALES_AND_TRAFFIC_REPORT',
'dataStartTime':'2022-03-10T20:11:24.000Z',
'dataEndTime':'2022-03-17T20:11:24.000Z',
'reportOptions':{
    'dateGranularity':'WEEK',
    'asinGranularity':'CHILD'}
       }

# Change data to json and add auth as additional parameter
reportId = requests.post("https://sellingpartnerapi-na.amazon.com/reports/2021-06-30/reports", 
           json=(data), 
           headers=headers, 
           auth=auth)

print(reportId)    
print(reportId.json())

RESPONSE:回复:

<Response [202]>

{'reportId': '54120019456'}  

This should result in giving you the reportId required.这应该会为您提供所需的 reportId。 Let me know if this helps.让我知道这是否有帮助。 Thanks谢谢

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

相关问题 sp-api - 卖家伙伴 api python - Sp-api - seller partner api python Amazon SP-API 速率限制 - Amazon SP-API Rate Limit 亚马逊卖家中心 - SP-API - 获取货件标签 - Amazon Seller Central - SP-API - GET Labels for shipment Amazon SP-API Python SellingApiForbiddenException:访问请求的资源被拒绝 - Amazon SP-API Python SellingApiForbiddenException: Access to requested resource is denied 亚马逊卖家中心 - SP-API - 创建提要文档 - InvalidInput - Amazon Seller Central - SP-API - Create a feed document - InvalidInput Amazon SP API Feeds API 处理报告:无效的产品类型 - Amazon SP API Feeds API processing report: Invalid product type Amazon SP API:Feeds API 处理报告神秘错误代码和消息 - Amazon SP API: Feeds API processing report Mystery error code and message Google Analytics Python API “收到无效的 JSON 有效载荷。 'report_requests[0]' 中的未知名称“segment”:找不到字段。” - Google Analytics Python API “Invalid JSON payload received. Unknown name ”segment“ at 'report_requests[0]': Cannot find field.” 中间件影响 API 上的所有请求 - Middleware affect all requests on an API Python请求无效的URL标签错误 - Python Requests Invalid URL Label error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM