OPNsense Backup with Python

Started by And1, October 17, 2023, 01:44:23 PM

Previous topic - Next topic
Hey,
I've recently started working with OPNsense. It's a larger project, and one part of it involves using a Python script to pull a backup of the current settings, which should then be available as an XML file. I already have this code:

python
Copy code
def backup():
    try:
       
        params = {'format': 'plain'}

        backup_url = f'{base_url}/backup/backup/download'
           
        try:
            response = requests.get(backup_url, auth = (api_key, api_secret), params=params, verify=False)
        except Exception as error:
            print(f'Error: {error}')

        if response.status_code == 200:
            with open('test_backup.xml', 'wb') as file:
                file.write(response.content)
                print("Backup successfully downloaded.")
        else:
            print(f"Error downloading the backup. Status code: {response.status_code}")

    except Exception as e:
        print(f'Error: {e}')

    pass
The API key, API secret, and the base URL are declared outside of the function. However, when I execute the code, I always receive a status code of 401. Can someone help me with my little script, or are there completely different approaches to implement this with Python? Please accept that im new in OPNsense, ff I've simply made a silly mistake, please forgive me. I hope someone can assist me. Thank you very much in advance.

And1

i solved it, the code is correct, but the backup_url must be:  f'{base_url}/backup/backup/download/?format=plain'

Greetings
And1