gpt4 book ai didi

python - 使用 python-requests 从 DHL 获取跟踪详细信息

转载 作者:太空宇宙 更新时间:2023-11-04 01:52:58 25 4
gpt4 key购买 nike

我正在尝试创建一个脚本,以通过 DHL API 获取我们公司所有订单的所有跟踪详细信息。

我尝试执行以下脚本以连接到 DHL API。

import requests
import json
import http.client

# Replace with the correct URL
url = "https://api-eu.dhl.com/track/shipments?trackingNumber=*************&requesterCountryCode=DE&originCountryCode=DE&language=en"
headers = {
'Accept': 'application/json',
'DHL-API-Key': '*********'
}
#connection = http.client.HTTPSConnection("https://api-eu.dhl.com")

myResponse = requests.get(url, headers)

if(myResponse.ok):
to fetch binary content
jData = json.loads(myResponse.content)

print("The response contains {0} properties".format(len(jData)))
print("\n")
for key in jData:
print (key + " : " + jData[key])
else:
with description
myResponse.raise_for_status()

但它显示以下错误,

Traceback (most recent call last):
File "/Users/sand/Documents/DHL Python.py", line 28, in <module>
myResponse.raise_for_status()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/requests/models.py", line 940, in raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: https://api-eu.dhl.com/track/shipments?trackingNumber=***************&requesterCountryCode=DE&originCountryCode=DE&language=en

所以我想澄清一下,

将其连接到 DHL 跟踪 API 需要做哪些事情?

我在 DHL 开发门户网站创建了一个帐户并填写了详细信息,对于选择 API 部分我只能选择 2,当我选择其他 API 时它显示为Ïn Progress",所以我删除了那个。

我得到了“消费者 key ”和“消费者 secret ”,我可以从哪里获得 token ,或者这足以连接 API?

除此之外我还需要做任何其他设置,因为我是新手,任何建议都会有很大帮助。

enter image description here

enter image description here单击隐藏使用者 key 的星号下方的显示链接。消费者 key == 'DHL-API-Key' 出现。


enter image description here

最佳答案

Question: get tracking details from DHL - do i need to do any other settings


From python-requests.org quickstart:

使用来自给定 DHL - Simple Python code samplerequests 而不是 http.client你必须做的:

import requests
url = "https://api-eu.dhl.com/track/shipments"

headers = {
'Accept': 'application/json',
'DHL-API-Key': 'ApiKeyHere'
}
payload = {
'trackingNumber': '7777777770',
'service': 'express'
}

# This url is for testing
url = 'https://httpbin.org/get'
resp = requests.get(url, params=payload, headers=headers)

print(resp.content)

Output: resp.content

{
"args": {
"service": "express",
"trackingNumber": "7777777770"
},
"headers": {
"Accept": "application/json",
"Accept-Encoding": "gzip, deflate",
"Dhl-Api-Key": "ApiKeyHere",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.22.0"
},
"origin": "54.224.8.86, 54.224.8.86",
"url": "https://httpbin.org/get?trackingNumber=7777777770&service=express"
}

使用 Python 3.6 测试 - python-requests/2.22.0

关于python - 使用 python-requests 从 DHL 获取跟踪详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57571131/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com