[Python] "Something wrong with request error" with DVLA MOT API GET Request [closed]

Discussão em 'Python' iniciado por Stack, Março 7, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to access the DVLA mot API. The OAuth 2 token request works and contact is being made with the server, but it returns error code MOTH-BR-01", "Something is wrong with the request" - no further explanation as to what is wrong. The code should return the vehicles details (Make, Model, MOT History) like so:

    {
    "registration": "string",
    "make": "Ford",
    "model": "Focus",
    "firstUsedDate": "2025-03-07",
    "fuelType": "Petrol",
    "primaryColour": "Silver",
    "registrationDate": "2025-03-07",
    "manufactureDate": "2025-03-07",
    "engineSize": "1598",
    "hasOutstandingRecall": "Yes",
    "motTests": [
    {
    "completedDate": "2023-02-17T09:17:46.000Z",
    "testResult": "PASSED",
    "expiryDate": "2025-03-07",
    "odometerValue": "string",
    "odometerUnit": "MI",
    "odometerResultType": "READ",
    "motTestNumber": "string",
    "dataSource": "DVSA",
    "defects": [
    {
    "text": "string",
    "type": "ADVISORY",
    "dangerous": true
    }
    ]
    },
    {
    "completedDate": "2023-02-17T09:17:46.000Z",
    "testResult": "PASSED",
    "expiryDate": "2025-03-07",
    "odometerValue": "string",
    "odometerUnit": "KM",
    "odometerResultType": "READ",
    "motTestNumber": "string",
    "dataSource": "DVA NI"
    },
    {


    however instead just returns an error message saying something was wrong with my request as follows:


    {"requestId": "773c4d80-9538-4063-9450-0323221b562c","errorCode":"MOTHBR-01", "errorMessage":"Something is wrong with the request"}



    Here is my code (With credentials and API key censored):

    import requests

    # Step 1: Get the OAuth2 token
    url = 'https://login.microsoftonline.com/ABC/oauth2/v2.0/token'
    headers = {
    'Content-Type': 'application/x-www-form-urlencoded'
    }

    # Define the data for the POST request to get the access token
    data = {
    'grant_type': 'client_credentials',
    'client_id': 'ABC',
    'client_secret': 'ABC',
    'scope': 'https://tapi.dvsa.gov.uk/.default'
    }

    # Send the POST request to get the token
    response = requests.post(url, headers=headers, data=data)

    # Check if the request was successful
    if response.status_code == 200:
    print('Access token:', response.json()['access_token'])
    token = response.json()['access_token']
    else:
    print('Error:', response.status_code, response.text)
    exit() # Exit if token request fails

    # Step 2: Request MOT history for the vehicle
    registration_number = 'ABC123' # Replace with the registration number of the vehicle you're querying
    url2 = f'https://history.mot.api.gov.uk/v1/trade/vehicles/{registration_number}'

    # Define the headers for the second request
    headers2 = {
    "Authorization": f"Bearer {token}", # Authorization header with access token
    "X-API-Key": "API_KEY" # API Key
    }

    # Send the GET request to get the MOT history
    response2 = requests.get(url2, headers=headers2) # GET request for MOT history

    # Check if the request was successful
    if response2.status_code == 200:
    print('MOT History:', response2.json())
    else:
    print(f"Error: {response2.status_code} - {response2.text}")


    Here is the API docs: https://documentation.history.mot.api.gov.uk/mot-history-api/api-specification/

    Thanks

    Continue reading...

Compartilhe esta Página