pythonpython-3.xpython-requestsdhl

Using python-requests to get tracking details from DHL


I'm trying to create a script to get all the tracking details of all orders from our company via DHL API.

I tried to execute the below script to connect to the 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()

But it is showing the below error,

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

So i want to clarify,

What all are the things that we need to do for connecting it to DHL tracking API?

I have created an account at DHL dev portal and filled the details,For the select API part i'm only able to select 2, when i'm selecting other API's then it is showing as Ïn Progress",So i removed that.

And i got "consumer Key" and "Consumer secret",From where i can get the token or this will be sufficient to connect the API?

Also apart from this do i need to do any other settings,Since i'm new to this any suggestions will be of great help.

enter image description here

enter image description here Click the Show link below the asterisks that is hiding the Consumer Key. The Consumer Key == 'DHL-API-Key' appears.


enter image description here


Solution

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


    From python-requests.org quickstart:

    Using requests instead of http.client from the given DHL - Simple Python code sample you have to do:

    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"
    }
    

    Tested with Python 3.6 - python-requests/2.22.0