2

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

stovfl
  • 14,998
  • 7
  • 24
  • 51
Sandeep
  • 671
  • 2
  • 7
  • 30
  • DHL does not use `HTTPBasicAuth`. Follow the section **Simple Python code sample** at [api-reference/shipment-tracking](https://developer.dhl/api-reference/shipment-tracking) – stovfl Aug 20 '19 at 13:44
  • @stovfl I have edited my question, Can you please let me know where to get the API key? – Sandeep Aug 21 '19 at 10:01
  • Your second image: Click the Show link below the asterisks that is hiding the Consumer Key. The Consumer Key == `'DHL-API-Key'` appears. – stovfl Aug 21 '19 at 10:32
  • @stovfl Ya,i'm trying witht hat but getting this error,Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 877, in _get_hostport port = int(host[i+1:]) ValueError: invalid literal for int() with base 10: '//api-eu.dhl.com' , Any suggestions on this? – Sandeep Aug 21 '19 at 10:54
  • This error ..raise InvalidURL("nonnumeric port: '%s'" % host[i+1:]) http.client.InvalidURL: nonnumeric port: '//api-eu.dhl.com' – Sandeep Aug 21 '19 at 10:54

1 Answers1

2

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

stovfl
  • 14,998
  • 7
  • 24
  • 51