Help getting to the /computer API endpoint

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Help getting to the /computer API endpoint

L1 Bithead
import csv import requests import getpass import urllib3   # ---------------------------- # Suppress SSL Warnings # ---------------------------- urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)   # ---------------------------- # Configuration # ---------------------------- csv_path = r"C:\Temp\ComputerName.csv" # Change to your file path api_version = "25.3.2" set_id = "moreblahblah-blah" base_url = f"https://BLAHBLAH.epm.cyberark.com/EPM/API/Sets/{set_id}" auth_url = "https://login.epm.cyberark.com/EPM/API/Auth/EPM/Logon" # US Dispatcher   # ---------------------------- # Prompt for Credentials # ---------------------------- username = input("Enter your EPM username: ") password = getpass.getpass("Enter your EPM password (input hidden): ")   # ---------------------------- # Authenticate with Dispatcher # ---------------------------- auth_payload = { "username": username, "password": password, "tenantId": tenant_id, "ApplicationID": "BlahApplication" }   headers = { "Content-Type": "application/json" }   auth_response = requests.post(auth_url, json=auth_payload, headers=headers, verify=False)   print("\nAuth Response Status code:", auth_response.status_code) print("Auth Response Body:", auth_response.text)   if auth_response.status_code == 200: auth_data = auth_response.json() token = auth_data.get("EPMAuthenticationResult") headers["Authorization"] = f"Bearer {token}" print(f"Authenticated. ApplicationID: {auth_data.get('ApplicationID')}") else: raise Exception(f"Failed to authenticate to EPM: {auth_response.status_code} - {auth_response.text}") #print(token)   # ---------------------------- # Read Computers from CSV # ---------------------------- with open(csv_path, mode='r', newline='', encoding='utf-8-sig') as file: reader = csv.DictReader(file) computers_to_delete = [ row['ComputerName'].strip().upper() for row in reader if row.get('ComputerName') ]   # ---------------------------- # Get Computers in the Set # ---------------------------- computers_url = f"{base_url}/Computers"   computers_response = requests.get(computers_url, headers=headers)   if computers_response.status_code != 200: raise Exception(f"Failed to retrieve computers: {computers_response.status_code} - {computers_response.text}")   computers = computers_response.json().get("Computers", [])   # Map: ComputerName → list of AgentIDs name_to_agent_ids = {} for comp in computers: name = comp["ComputerName"].upper() agent_id = comp["AgentID"] name_to_agent_ids.setdefault(name, []).append(agent_id)   # ---------------------------- # Delete Matching Computers # ---------------------------- for comp_name in computers_to_delete: agent_ids = name_to_agent_ids.get(comp_name) if agent_ids: for agent_id in agent_ids: delete_url = f"{base_url}/Computers/{agent_id}" delete_response = requests.delete(delete_url, headers=headers, verify=False)   if delete_response.status_code == 200: print(f"Deleted {comp_name} (AgentID: {agent_id})") else: print(f"Failed to delete {comp_name} (AgentID: {agent_id}): {delete_response.status_code} - {delete_response.text}") else: print(f"{comp_name} not found in Set '{set_name}'.")

Anyone know what is wrong my python script? Getting an error "  raise Exception(f"Failed to retrieve computers: {computers_response.status_code} - {computers_response.text}")

Exception: Failed to retrieve computers: 404 - [{"ErrorCode":"EPM000004E","ErrorMessage":"The requested resource could not be found, or the user does not have permissions for this resource."}]"

 

 

4 REPLIES 4

L7 Applicator

Hi @Henrique da Costa​ 

 

It seems base_url or set_id is incorrect or in case authenticating user doesn't have the right permissions to view endpoints in that set, you'll also get this error.

 

Hope it helps !

 

L2 Linker

Hello,

After authentication you provide the token as bearer while it should be basic

Try Authorization: basic <TOKEN>

 

https://docs.cyberark.com/epm/latest/en/content/webservices/getcomputers.htm#Headerparameters

 

Have a nice day.

L2 Linker

Hello,

Try omitting the dollar sign, keep only the question mark

...Computers?limit=3000

 

L1 Bithead

OMG! That was freaking it. Got everything to work that way I needed. Thank you so much.

  • 156 Views
  • 4 replies
  • 0 Likes
Like what you see?

Show your appreciation!

Click Like if a post is helpful to you or if you just want to show your support.

Click Accept as Solution to acknowledge that the answer to your question has been provided.

The button appears next to the replies on topics you’ve started. The member who gave the solution and all future visitors to this topic will appreciate it!

These simple actions take just seconds of your time, but go a long way in showing appreciation for community members and the LIVEcommunity as a whole!

The LIVEcommunity thanks you for your participation!