所有报告请求的 SP-API "Invalid Input"错误



当我向'Inventory'或'Sales'端点发出请求时,我得到了成功的响应。这证实了我的签名和我的身份是正确的。我正在手动签名请求,因为我不确定如何使用boto3来创建签名。

我不确定为什么,但是当发出POST请求时,响应将声明The Canonical String for this request should have been...并提供下面的payload_hash。我复制/粘贴字符串提供。一旦我这样做了,我就得到了InvalidInput响应。然而,当使用GET请求时,我能够使用payload_hash = hashlib.sha256(('').encode('utf-8')).hexdigest(),如下所示,它工作得很好。

这与GET_SALES_AND_TRAFFIC_REPORT和其他报告的响应相同。

创建规范字符串:

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

请求:

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 [400]>
{
"errors": [
{
"code": "InvalidInput",
"message": "Invalid Input",
"details": ""
}
]
}

花了我一段时间让签名的东西在邮差工作,只是看看你的请求,我相信MarketplaceIds需要是一个数组,虽然你会得到一个特定的错误,如果这是主要问题:

"marketplaceIds": [
"ATVPDKIKX0DER"
]

我正在做这个。所以,我使用AWS4Auth来创建一个规范字符串,只是为了确保我没有弄乱它。

这样做的方法:(假设您已经生成了LWA访问令牌,我可以在您的标题中看到'访问')

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 [202]>
{'reportId': '54120019456'}  

这将为您提供所需的reporttid。如果有帮助,请告诉我。由于

相关内容

最新更新