API FedEX“INVALID.INPUT.EXCEPTION”,“message”:“输入中的字段值无效”

发布于 2025-01-09 01:34:41 字数 1631 浏览 0 评论 0原文

我正在尝试使用 Python 3.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"}]}

我已经进行了大量测试,但没有收到无效字段。 有人知道发生了什么事并且可以提供帮助吗?

I'm trying to validade an address in FedEX API using Python 3.8 and it returns an error of invalid field value

First I connect to the 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)

And it returns a message with the Auth token correctly

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

Then I just get the token to use it in next transactions

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

Then I prepare the next payload for address validation API

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

And send the request to the new endpoint with the given token

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)

and get the error

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

I have made inumerous tests and I don't get the invalid field.
Anyone know what is happening and can help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦开始←不甜 2025-01-16 01:34:41

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

payload = json.dumps({input payload}) this works to prevent the response error 422
<Response [422]>

听风吹 2025-01-16 01:34:41

我已经修复了它

出于任何原因,在 2 个步骤中将字符串 Payload_valid_address 转换为 Json 是行不通的,

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

但如果仅一步完成,它就会传递 API 请求

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

I have fixed it

For any reason, convert the string payload_valid_address to Json in 2 steps doesn't work

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

But if made in just one step it pass the API request

payload_valid_address = json.dumps({....})
葬心 2025-01-16 01:34:41

我也遇到了这个错误,并且当 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)

I also had this error and additionally had to use json.loads(json_string) when the json data contained key words like false, true or null.

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文