This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Problem with an API script

Problem with an API script
Hello everyone.
I have a python script that authenticates through the API by creating an access_token.txt file, file session_cookies.txt and another id_value.txt file for the Sophos Enterprise console.
Then I run a second script to query data from the sub-states and it fails. Returns a 404 error.
My intention is to obtain the list of sub-states, be able to choose a specific one and obtain information about the endpoints.
Can somebody help me?

Script1

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import requests
import json
# Obtener el access token
Client_ID = "yyyyyyyyyyyyyyyyyyyyyyyyyyy"
Client_Secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
url = "https://id.sophos.com/api/v2/oauth2/token"
payload = {'grant_type': 'client_credentials', 'client_id': Client_ID, 'client_secret': Client_Secret, 'scope': 'token'}
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
response = requests.post(url, headers=headers, data=payload, verify=True)
# Verificar si la autenticación fue exitosa
if response.status_code == 200:
response_data = json.loads(response.text)
access_token = response_data['access_token']
# Escribir el access token en un archivo
with open('access_token.txt', 'w') as f:
f.write(access_token)
# Leer el token del archivo access_token.txt
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Script2

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import requests
import csv
import json
# Leer las cookies de sesión del archivo
with open('session_cookies.txt', 'r') as f:
saved_cookies = json.loads(f.read())
cookies = requests.utils.cookiejar_from_dict(saved_cookies)
# Hacer una solicitud para obtener la lista de subtenants
url = "https://api.central.sophos.com/v1/tenants"
response = requests.get(url, cookies=cookies)
# Verificar si la solicitud fue exitosa
if response.status_code == 200:
data = response.json()
# Crear un listado de los subtenants en un fichero csv
with open('subtenants.csv', 'w', newline='') as csvfile:
fieldnames = ['Nombre', 'ID']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX



This thread was automatically locked due to age.