I wrote a script to download the log in Synology NAS active backup for M365. Manually if I click on export everything works. But I need to download it with the script.
The authentication on the NAS is OK, I debugged it. The file downloads but there is an error message inside the code: {“error”:{“code”:101},”success”:false}
I’m stuck, I’m attaching the code, does anyone have any experience with this? It must be possible to download it automatically using a script.
Thank you very much for your help and any advice. Code written in python
enter image description here – screenshot
`https://pastebin.com/EU1RbCQM`
import requests
import urllib3
# Suppress InsecureRequestWarning
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
def get_json(url):
response = requests.get(url, verify=False)
return response.json()
def download_file(url, sid, file_path):
headers = {'Cookie': f'id={sid}'}
response = requests.post(url, headers=headers, verify=False)
if response.status_code == 200:
with open(file_path, 'wb') as file:
file.write(response.content)
print(f"File successfully downloaded to: {file_path}")
else:
print("Failed to download the file.")
def main():
host = "xxx"
user = "xxx"
password = "xxx."
# Login and obtain session ID
login_url = f"https://{host}/webapi/auth.cgi?api=SYNO.API.Auth&method=login&version=3&account={user}&passwd={password}&session=FileStation&format=sid"
login_response = get_json(login_url)
print(f"Login response: {login_response}")
if "data" in login_response and "sid" in login_response["data"]:
sid = login_response["data"]["sid"]
print(f"Login successful. Session ID: {sid}")
# URL to download the file
download_url = f"https://{host}/webapi/entry.cgi?api=SYNO.ActiveBackupOffice365&method=download_file&version=1&SynoHash=bWP_dbnWIHl-lmFtCp1qNIvpoRwSrA.NzI&SynoToken=.vLMwHcu.wwuE"
file_path = "C:\scripts\ActiveBackup_Activity_Log.csv"
# Download the file
download_file(download_url, sid, file_path)
# Logout
logout_url = f"https://{host}/webapi/auth.cgi?api=SYNO.API.Auth&method=logout&version=3&session=FileStation&_sid={sid}"
logout_response = get_json(logout_url)
if logout_response["success"]:
print("Logout successful.")
else:
print("Logout failed.")
else:
print("Login failed. Check the username, password, or API endpoints.")
if __name__ == "__main__":
main()
Aladin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.