API 联邦快递 "INVALID.INPUT.EXCEPTION","message":"Invalid field value in the input"

API FedEX "INVALID.INPUT.EXCEPTION","message":"Invalid field value in the input"

我正在尝试使用 Python 3.8 验证 FedEX API 中的一个地址,它 returns 一个无效字段值错误

首先我连接到 Auth API

payload={"grant_type": "client_credentials",'client_id':Client_id,'client_secret':Client_secret}
url = "https://apis-sandbox.fedex.com/oauth/token"
headers = {'Content-Type': "application/x-www-form-urlencoded"}
response=requests.post(url, data=(payload), headers=headers)

它 returns 一条带有正确验证令牌的消息

{"access_token":"eyJhbGciOiJSUzI1NiIsInRM5U0F2eUs1ZVFBVTFzS5k","token_type":"bearer","expires_in":3599,"scope":"CXS SECURE"}

然后我就得到令牌在下一次交易中使用它

token = json.loads(response.text)['access_token']

然后我准备下一个有效载荷用于地址验证API

payload_valid_address = {
    "addressesToValidate": [
        {
    "address":
            {
            "streetLines": ["7372 PARKRIDGE BLVD"],
            "city": "Irving",
            "stateOrProvinceCode": "TX",
            "postalCode": "75063-8659",
            "countryCode": "US"
            }
        }
    ]
}

并使用给定的令牌将请求发送到新端点

url = "https://apis-sandbox.fedex.com/address/v1/addresses/resolve"
headers = {
    'Content-Type': "application/json",
    'X-locale': "en_US",
    'Authorization': 'Bearer '+ token
    }

response = requests.post(url, data=payload_valid_address, headers=headers)

print(response.text)

并得到错误

<Response [422]>
{"transactionId":"50eae03e-0fec-4ec7-b068-d5c456b64fe5","errors":[{"code":"INVALID.INPUT.EXCEPTION","message":"Invalid field value in the input"}]}

我做了无数次测试,但没有得到无效字段。 任何人都知道发生了什么并且可以提供帮助?

我已经修复了

出于任何原因,分两步将字符串 payload_valid_address 转换为 Json 无效

payload_valid_address = {....}
payload = json.dumps(payload_valid_address)

但如果仅一步完成,它就会通过 API 请求

payload_valid_address = json.dumps({....})

payload = json.dumps({input payload}) 这可以防止响应错误 422 <响应 [422]>