我试图使用Python3.8在FedEX API中验证一个地址,它返回一个无效字段值的错误
首先,我连接到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)
它正确地返回带有Auth令牌的消息。
{"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"}]}
我做了大量的测试,但没有得到无效字段。有人知道发生了什么,能帮上忙吗?
发布于 2022-03-17 09:43:25
有效载荷=json.dumps({输入有效载荷})这是为了防止响应错误422 <响应422>
发布于 2022-02-24 17:52:11
我已经修好了
由于任何原因,在两个步骤中将字符串payload_valid_address转换为Json是行不通的
payload_valid_address = {....}
payload = json.dumps(payload_valid_address)
但是,如果只在一步内完成,它就会通过API请求。
payload_valid_address = json.dumps({....})
发布于 2022-07-22 19:28:54
此外,当json数据包含诸如false、true或null等关键字时,我还必须使用json.loads(json_string)
。
import json
json_string = r'''{
"accountNumber": {
"value": "802255209"
},
"requestedShipment": {
"shipper": {
"address": {
"postalCode": 75063,
"countryCode": "US"
}
},
"recipient": {
"address": {
"postalCode": "m1m1m1",
"countryCode": "CA"
}
},
"shipDateStamp": "2020-07-03",
"pickupType": "DROPOFF_AT_FEDEX_LOCATION",
"serviceType": "INTERNATIONAL_PRIORITY",
"rateRequestType": [
"LIST",
"ACCOUNT"
],
"customsClearanceDetail": {
"dutiesPayment": {
"paymentType": "SENDER",
"payor": {
"responsibleParty": null
}
},
"commodities": [
{
"description": "Camera",
"quantity": 1,
"quantityUnits": "PCS",
"weight": {
"units": "KG",
"value": 20
},
"customsValue": {
"amount": 100,
"currency": "USD"
}
}
]
},
"requestedPackageLineItems": [
{
"weight": {
"units": "KG",
"value": 20
}
}
]
}
}'''
data = json.loads(json_string)
data
response = requests.request("POST", url, data=json.dumps(data), headers=headers)
https://stackoverflow.com/questions/71228838
复制相似问题