Looking for Python example to get authorization token with API

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

Welcome Idira to LIVEcommunity!

Our community just got stronger! Explore new identity security resources, connect with experts, and discover more ways to learn and collaborate.

Meet Idira

Looking for Python example to get authorization token with API

L1 Bithead

I took the Python - Requests code from Postman and ran it in Python IDE. I can get the token with the payload string from Postman with the actual username and password.

 

payload = "{\n\t\"username\": \"APIuser\",\n\t\"password\": \"APIpw\",\n\t\"connectionNumber\": \"1\"\n}"

 

However, I have not been able to find the right syntax to be able to pass in the above example APIuser and APIpw as variables. If I do a print on payload, I see the variable values are being passed, however I am receiving the below error:

 

b'\xc3\xaf\xc2\xbb\xc2\xbf<?xml version="1.0" encoding="utf-8"?>\r\n<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\r\n<html xmlns="http://www.w3.org/1999/xhtml">\r\n <head>\r\n   <title>Request Error</title>\r\n   <style>BODY { color: #000000; background-color: white; font-family: Verdana; margin-left: 0px; margin-top: 0px; } #content { margin-left: 30px; font-size: .70em; padding-bottom: 2em; } A:link { color: #336699; font-weight: bold; text-decoration: underline; } A:visited { color: #6699cc; font-weight: bold; text-decoration: underline; } A:active { color: #336699; font-weight: bold; text-decoration: underline; } .heading1 { background-color: #003366; border-bottom: #336699 6px solid; color: #ffffff; font-family: Tahoma; font-size: 26px; font-weight: normal;margin: 0em 0em 10px -20px; padding-bottom: 8px; padding-left: 30px;padding-top: 16px;} pre { font-size:small; background-color: #e5e5cc; padding: 5px; font-family: Courier New; margin-top: 0px; border: 1px #f0f0e0 solid; white-space: pre-wrap; white-space: -pre-wrap; word-wrap: break-word; } table { border-collapse: collapse; border-spacing: 0px; font-family: Verdana;} table th { border-right: 2px white solid; border-bottom: 2px white solid; font-weight: bold; background-color: #cecf9c;} table td { border-right: 2px white solid; border-bottom: 2px white solid; background-color: #e5e5cc;}</style>\r\n </head>\r\n <body>\r\n   <div id="content">\r\n     <p class="heading1">Request Error</p>\r\n     <p>The server encountered an error processing the request. See server logs for more details.</p>\r\n   </div>\r\n </body>\r\n</html>'

 

Please share any working examples

Thanks,

Shawn

3 REPLIES 3

L4 Transporter

import requests

import time

import getpass

import base64

 

# Enter login Credentials to authenticate

try:

  username= getpass.getpass("Enter your vault username:")

  password = base64.b64encode(getpass.getpass("Enter vault Password:"))

except:

  print ('ERROR', err)

url="https://PVWA/PasswordVault/WebServices/auth/Cyberark/CyberArkAuthenticationService.svc/Logon"

headers = { 'Content-Type': 'application/json','Cache-Control': 'max-age=300'}

data = '{ "username":"'+username+'","password":"'+base64.b64decode(password)+'",  "useRadiusAuthentication":"false", "connectionNumber":"1"}'

response = requests.request("POST",url, headers=headers, data=data, verify=False)

output_response=response.json()

auth_token=output_response['CyberArkLogonResult']

print auth_token

 

 

# Enter Account Details 

try:

  username= getpass.getpass("Enter serivce Account name:")

  Password= getpass.getpass("Enter Serivce Account Password:")

except:

  print ('ERROR', err)

 

url = "https://PVWA/PasswordVault/WebServices/PIMServices.svc/Account"

headers = { 'Content-Type': 'application/json','Authorization':auth_token}

data = '{"account" : { "safe":"XXXXXXXX","platformID":"XXXXXXXX","address":"XXXXXX","username":"'+username+'","password":"'+Password+'","disableAutoMgmt":"false","Port":"XXXXX","Database":"XXXXX" }}'

response = requests.request("POST",url, headers=headers, data=data, verify=False)

time.sleep(1)

#print (response.text)

statusCode=response.status_code

if statusCode == 201:

  print "Account Added succesfully:{}".format(statusCode)

else:

  print " Account Failed to Add: {}".format(statusCode)

   

# logout of the vault 

url="https://XXXXXXXX/PasswordVault/WebServices/auth/Shared/RestfulAuthenticationService.svc/Logoff"

headers = { 'Content-Type': 'application/json','Authorization':auth_token}

response = requests.request("POST", url, headers=headers, verify=False)

time.sleep(1)

logoff_response=response.status_code

 

if logoff_response == 200:

  print "Session terminated with status_code: {}".format(logoff_response)

else:

  print "Session is NOT terminated and default timeout is 30 minutes. status_code: {}".format(logoff_response)

 

 

Rag,

 

Thank you!! This resolved my issue.

L1 Bithead

@Anurag .​  Thank You! This Helps.

  • 2203 Views
  • 3 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!