Getting the API token is fine but getting to restore the backup does not work. Since this is not a token error, I suppose that this from the headers of the requests.
My code :
import requests
def get_token(email, password):
url = "<IP>/api/v1/login"
headers = {
"Accept": "application/json, text/plain, */*",
"X-React": "true",
"X-Requested-With": "XMLHttpRequest",
"X-Api-Token": "null",
"User-Agent": "<user-agent>",
"Content-Type": "application/json",
"Origin": "<IP>",
"Referer": "<IP>",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "q=0.9,en-US;q=0.8,en;q=0.7",
"Connection": "close",
}
data = '{{"email":"{0}","password":"{1}","one_time_password":"","secret":""}}'.format(email, password)
response = requests.post(url, headers=headers, data=data)
try:
json_response = response.json()
token_info = json_response['data'][0]['token']
token = token_info['token']
print("Token:", token)
return token
except Exception as e:
print(f"Error parsing JSON response: {e}")
def post_backup_via_api(token, file_path):
url = "<IP>/api/v1/import_json?import_settings=true&import_data=true"
headers = {
"Accept": "application/json, text/plain, */*",
"X-React": "true",
"X-Requested-With": "XMLHttpRequest",
"X-Api-Token": token,
"User-Agent": "<user-agent>",
"Content-Type": "multipart/form-data; boundary=----WebKitFormBoundaryMlUW4HSzLDMUKTT8",
"Origin": "<IP>",
"Referer": "<IP>/settings/backup_restore/restore",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "q=0.9,en-US;q=0.8,en;q=0.7",
}
files = {'file': ('invoice_ninja.zip', open('invoice_ninja.zip', 'rb'), 'application/zip')}
response = requests.post(url, headers=headers, files=files)
try:
json_response = response.json()
print(json_response)
except Exception as e:
result = f"Error parsing JSON response: {e}"
print(result)
def main_invoice_ninja_backup_via_api(email, password, file_path):
token = get_token(email, password)
post_backup_via_api(token, file_path)
if __name__ == "__main__":
email = "[email protected]"
password = "password"
file_path = "invoice_ninja.zip"
main_invoice_ninja_backup_via_api(email, password, file_path)
Here is a sample of the response from the API :
{
"message":"Call to a member function storeAs() on null",
"exception":"Error",
"file":"/var/www/app/app/Http/Controllers/ImportJsonController.php",
"line":62,
"trace":[
{
"file":"/var/www/app/vendor/laravel/framework/src/Illuminate/Routing/Controller.php",
"line":54,
"function":"import",
"class":"App\\Http\\Controllers\\ImportJsonController",
"type":"->"
},
{
"file":"/var/www/app/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php",
"line":43,
"function":"callAction",
"class":"Illuminate\\Routing\\Controller",
"type":"->"
},
{
"file":"/var/www/app/vendor/sentry/sentry-laravel/src/Sentry/Laravel/Tracing/Routing/TracingControllerDispatcherTracing.php",
"line":21,
"function":"dispatch",
"class":"Illuminate\\Routing\\ControllerDispatcher",
"type":"->"
}
....
Any insights ?