繁体   English   中英

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

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

当我向“库存”或“销售”端点发出请求时,我得到了成功的响应。 这证实了我的签名和 IAM 是正确的。 我正在手动签署请求,因为我不确定如何使用 boto3 创建签名。

我不知道为什么,但是在发出 POST 请求时,响应将 state 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 访问令牌,我可以在您的 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 [202]>

{'reportId': '54120019456'}  

这应该会为您提供所需的 reportId。 让我知道这是否有帮助。 谢谢

暂无
暂无

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

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