How can I prevent error 401 when making a DHL tracking request?

82 Views Asked by At

I just registered with DHL and created a tracking app on their developer portal. I wrote the API call in python and am getting the following error:

Error: 401, {"status":401,"title":"Unauthorized","detail":"Unauthorized for given resource."}

Here is my code with API Key removed:

import re
import requests

from requests.auth import HTTPBasicAuth

import requests

def track_dhl_package(tracking_number, api_key, service):
    headers = {
        'Accept': 'application/json',
        'DHL-API-Key': api_key
    }
    # Include the 'service' parameter in the query string
    url = f'https://api-eu.dhl.com/track/shipments?trackingNumber={tracking_number}&service={service}'
    
    response = requests.get(url, headers=headers)
    
    if response.status_code == 200:
        return response.json()
    else:
        # Including response.text to provide more details on why the request failed
        return f"Error: {response.status_code}, {response.text}"

# Example usage
tracking_number = "927489032613xxxxxxxxxx"
api_key = "######################"
service = "ecommerce"  # Adjust based on the actual DHL service used for your shipment

print(track_dhl_package(tracking_number, api_key, service))

Does anyone have any ideas what I'm doing wrong? The app says I have no defined rate limit.

enter image description here

0

There are 0 best solutions below