联邦快递API Python:不存在参考跟踪号

发布于 2025-01-31 09:53:07 字数 1906 浏览 1 评论 0原文

最终目标是使用FedEx API读取跟踪数字的电子表格,然后通过FedEx API更新其运输状态

。第一步是尝试通过API读取跟踪号的状态。要实现这一目标,请遵循通过跟踪编号 FedEx提供的文档。应该注意的是,有一个测试环境生产环境在我仍在开发时,我的应用程序处于测试环境中。

在测试环境中,您将限制您使用FedEx的预定义的跟踪号“ nofollow noreferrer”>模拟跟踪数字列表。

执行以下脚本和输入数字形式时,我们可以将错误或成功分类为2种响应类型,一个错误或成功,其中错误指示跟踪号不存在,并且成功

import json
import requests #used to make the HTTP request


SecretKey = "REDACTED"
PublicKey = "REDACTED"
 

#Make a call to the Authrization Endpoint
AuthURL = "https://apis-sandbox.fedex.com/oauth/token"
AuthHeader = {
    'Content-Type': "application/x-www-form-urlencoded" #Content-Type tells what form the body takes
    }
AuthBody ={
    "client_id": PublicKey,
    "client_secret": SecretKey,
    "grant_type": "client_credentials"
}
AuthResponse = requests.request("POST",url=AuthURL,data=AuthBody,headers=AuthHeader)
AuthToken = json.loads(AuthResponse.text)["access_token"]

#Make a call to the Tracking Endpoint
TrackingNumber = input("What are you Tracking? ")
TrackURL = "https://apis-sandbox.fedex.com/track/v1/trackingnumbers"
TrackHeader ={
    "content-type": "application/json",
    "authorization": "Bearer "+AuthToken
}
TrackBody ={
    "includeDetailedScans": True,
    "trackingInfo": [
        {
        "trackingNumberInfo": {
            "trackingNumber": TrackingNumber
            }
        }
    ]
}
print("Requesting Data...")
RawTrackingResponse = requests.request("POST",url=TrackURL,data=json.dumps(TrackBody),headers=TrackHeader)
print(json.loads(RawTrackingResponse.text))

使用 FedEx文档中所述的正文响应跟踪编号 797806677146 从列表中,我们

在使用跟踪号码 920241085725456

我们 会收到成功的全部响应。是联邦快递API还是我的代码问题?

The end goal is to use the Fedex API to read a spreadsheet of tracking numbers and then update their shipping status via the Fedex API

The first step was to attempt to read the status of a tracking number via the API. To accomplish this is am following the Track by Tracking Number documentation that Fedex has provided. It should be noted that there is a Test Environemnt and a Production Environemnt as I am still developing, my app is in the test environemnt.

Being in the Test environment limits you to use FedEx's predefined tracking numbers as described in the Mock Tracking Numbers list.

When executing the following script and inputting numbers form the predefined list we can categorize into 2 response types, an error or a success where an error indicates the tracking number does not exist and a success responds with the body as described in the fedex documentation

import json
import requests #used to make the HTTP request


SecretKey = "REDACTED"
PublicKey = "REDACTED"
 

#Make a call to the Authrization Endpoint
AuthURL = "https://apis-sandbox.fedex.com/oauth/token"
AuthHeader = {
    'Content-Type': "application/x-www-form-urlencoded" #Content-Type tells what form the body takes
    }
AuthBody ={
    "client_id": PublicKey,
    "client_secret": SecretKey,
    "grant_type": "client_credentials"
}
AuthResponse = requests.request("POST",url=AuthURL,data=AuthBody,headers=AuthHeader)
AuthToken = json.loads(AuthResponse.text)["access_token"]

#Make a call to the Tracking Endpoint
TrackingNumber = input("What are you Tracking? ")
TrackURL = "https://apis-sandbox.fedex.com/track/v1/trackingnumbers"
TrackHeader ={
    "content-type": "application/json",
    "authorization": "Bearer "+AuthToken
}
TrackBody ={
    "includeDetailedScans": True,
    "trackingInfo": [
        {
        "trackingNumberInfo": {
            "trackingNumber": TrackingNumber
            }
        }
    ]
}
print("Requesting Data...")
RawTrackingResponse = requests.request("POST",url=TrackURL,data=json.dumps(TrackBody),headers=TrackHeader)
print(json.loads(RawTrackingResponse.text))

When using tracking number 797806677146 from the list we receive a success full response

When using tracking number 920241085725456 we get an error

Why do some of the reference codes work and others do not? is it an issue with the fedex api or my code?

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

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

发布评论

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

评论(1

回忆躺在深渊里 2025-02-07 09:53:07

我相信联邦快递如今仅使用12位跟踪号。您无效的电话是15位数字。我记得,FedEx曾经有15位的跟踪号码,因此您阅读的文档也许是旧的。

顺便说一句,尽管您的代码使用FedEx沙箱,但它可以与真实的跟踪数字一起使用。我只是用真实的(即不是沙盒)跟踪编号尝试了它 - 没有问题。

I believe FedEx is only using 12-digit tracking numbers these days. Your number that doesn't work is 15 digits. As I recall, FedEx used to have 15-digit tracking numbers so perhaps the documentation you read is old.

BTW, though your code uses the FedEx sandbox, it can work with real tracking numbers. I just tried it with a real (i.e. not sandbox) tracking number - no problems.

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