【发布时间】:2022-07-23 03:28:54
【问题描述】:
我正在尝试使用 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"}]}
我进行了无数次测试,但没有得到无效字段。 有谁知道发生了什么并可以提供帮助?
【问题讨论】:
-
FedEX 使用 OAuth 2.0 令牌身份验证方法来授权应用程序并验证 API 请求。
-
尝试使用来自
json的string对象:import json payload_valid_address = '''{ "addressesToValidate": [ { "address": { "streetLines": ["7372 PARKRIDGE BLVD"], "city": "Irving", "stateOrProvinceCode": "TX", "postalCode": "75063-8659", "countryCode": "US" } } ] }'''PAYLOAD_VALID_ADDRESS = json.load(PAYLOAD_VALID_ADDRESS) -
同样的问题。我尝试将其他端点 API 与其他有效负载一起使用,但遇到了同样的问题。另一点是错误 422 未在 FedEX 文档及其 Json Schema 中列出。我认为问题不在于有效负载,而在于我访问 API 或验证令牌的方式。