how to retrieve credentials from CCP API using Python and OS username

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

how to retrieve credentials from CCP API using Python and OS username

L2 Linker

Hello,

I'm a newbie In Cyberark CCP and I wondering how can I get the credential from CCP with OS username authentication.

I can get the credential by Powershell script like this

$appid = 'My_APP' $safe = 'My_Safe' $credtofetch = 'My_Account' #execute REST API call [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 invoke-restmethod -Credential (get-credential) -uri https://abc.mydomain.com/aimwebservice/api/accounts -method GET -Body @{AppID = $appid; Safe = $safe; UserName = $credtofetch; Reason = 'Powershell'}

But I cannot find the way to get the credential when using python code.

my code always return 403 with error "APPAP133E Failed to verify application authentication data: OSUser \\" \\" is unauthorized"

 

Python code

import requests from requests_ntlm import HttpNtlmAuth import getpass import ssl # Replace these values with your API endpoint and Windows domain certificate_path = 'C:MyClientCert\certificate.pem' ssl_context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH) ssl_context.load_cert_chain(certfile=certificate_path) api_url = 'https://abc.mydomain.com/AIMWebService/api/Accounts?AppID=my_app&Safe=My_safe&Object=My_Account' domain = 'mydomain.com' # Get the current Windows username windows_username = getpass.getuser()   # Prompt for the Windows password windows_password = getpass.getpass(f"Enter the password for {windows_username}@{domain}: ")   # Make a request to the API using NTLM authentication response = requests.get(api_url, auth=HttpNtlmAuth(f'{domain}\\{windows_username}', windows_password),cert=certificate_path, verify=False)   # Check the response if response.status_code == 200: print("Successfully authenticated.") print("Response:") print(response.text) else: print(f"Failed to authenticate. Status code: {response.status_code}")

does anyone have succeeded with this aspect can give me your Python code?

5 REPLIES 5

L4 Transporter

@Hiep Nguyen?  This article should help you resolve your issue

https://community.cyberark.com/s/article/CCP-AIMWebService-Windows-Authentication-not-working

L7 Applicator

Hi @Hiep Nguyen​ 

 

The error you're encountering indicates an authorisation issue, and it seems to be related to the application authentication. Please verify that you have the necessary permissions to access the specified account in the Safe, and that your application has the required privileges.

 

Please refer to the link shared by @Vinay Vasanth​ 

 

The Python script, it looks good to me. There are couple of observations.

 

1.The requests_ntlm library already handles NTLM authentication, so you don't need to manually construct the authentication string

 

Change the line,

 

response = requests.get(api_url, auth=HttpNtlmAuth(f'{domain}\\{windows_username}', windows_password), cert=certificate_path, verify=False)

 

To

 

response = requests.get(api_url, auth=HttpNtlmAuth(f'{windows_username}@{domain}', windows_password), cert=(certificate_path, certificate_path), verify=False)

 

2.It's not recommended to disable SSL verification (verify=False). Instead, provide the path to the CA certificate used by the server

 

Hope it helps !

 

 

Hello @Vinay Vasanth​ . All configurations based on the document have been set already. but it is still the same. and the problem is Python code does not work, but Powershell still works correctly

 

Working Python script without explicitly specifying the username / password for OS Authentication

 

Note: Ensure that CCP server and the server from where you execute this script are in same domain.

 

 

import win32com.client, json

 

Host = "https://pvwauat.com/AIMWebService/api/Accounts"

AppID = "DiscoveryScanApplication"

Safe = "DiscoveryScanAppWindows"

Folder = "Root"

Object = "Operating System-AD-ERDRSSD40D.COMPANY-HPDRDTAN"

 

url = Host+'?AppID='+AppID+'&Folder='+Folder+'&safe='+Safe+'&object='+Object

 

httpsRequest = win32com.client.Dispatch('WinHTTP.WinHTTPRequest.5.1')

httpsRequest.SetAutoLogonPolicy(0)

httpsRequest.Open('GET', url, False)

httpsRequest.Send()

result = httpsRequest.responseText

 

# Check the response

if httpsRequest.Status == 200:

  print("Successfully authenticated.")

  jsonResponse = json.loads(result)

  username = jsonResponse.get('UserName')

  password = jsonResponse.get('Content')

  print("UserName : " + username + " Password : " + password)

else:

  print(f"Failed to authenticate. Status code: {httpsRequest.Status}")

L3 Networker

Working Python script without explicitly specifying the username / password for OS Authentication

 

Note: Ensure that CCP server and the server from where you execute this script are in same domain.

 

 

import win32com.client, json

 

Host = "https://pvwauat.com/AIMWebService/api/Accounts"

AppID = "DiscoveryScanApplication"

Safe = "DiscoveryScanAppWindows"

Folder = "Root"

Object = "Operating System-AD-ERDRSSD40D.COMPANY-HPDRDTAN"

 

url = Host+'?AppID='+AppID+'&Folder='+Folder+'&safe='+Safe+'&object='+Object

 

httpsRequest = win32com.client.Dispatch('WinHTTP.WinHTTPRequest.5.1')

httpsRequest.SetAutoLogonPolicy(0)

httpsRequest.Open('GET', url, False)

httpsRequest.Send()

result = httpsRequest.responseText

 

# Check the response

if httpsRequest.Status == 200:

  print("Successfully authenticated.")

  jsonResponse = json.loads(result)

  username = jsonResponse.get('UserName')

  password = jsonResponse.get('Content')

  print("UserName : " + username + " Password : " + password)

else:

  print(f"Failed to authenticate. Status code: {httpsRequest.Status}")

  • 1200 Views
  • 5 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!