Unable to generate OTCS token through Rest API Python Script

Hi All,

i was trying to generate OTCS token so that i can authenticate and write a script for business workspace creation. Through postman i tested the API and works fine. Referring to that, i wrote a python script. I can see that authentication or token generation is not happening. am posting my code below.As i am pretty new to Python and incase Anyone came across similar issues, please guide me. Right now its just executing the catch block.

import requests

import http
def get_ticket():    auth_url = "http://CS URL/OTCS/cs.exe/api/v1/auth"   

OTCSToken = {  'Username = username & Password = pass'    } 

  headers = {  'Content-Type': 'application/x-www-form-urlencoded'}   

response = requests.request("POST",auth_url,headers=headers, data=OTCSToken)   

response_data = response.json()   

if 'access_token' in response_data:       

return response_data['access_token']   

else:        raise ValueError("Failed to obtain access token")

def main():    try:       

 OTCSToken = get_ticket()       

print('Tokren Generated {OTCSToken}')

except Exception as e:       

print('An error occurred {e}')

if __name__=='__main__':    main()

Tagged:

Comments

  • appuq
    appuq Member

    my postman suggested this I see in yours your payload is a JSON, in mine it is a string . Sorry, I am not a python programmer.

    import requests
    url = "https://myserver:8443/otcs/cs.exe"
    payload = 'username=Admin&password=*******!'

    headers = {  'Content-Type': 'application/x-www-form-urlencoded'}
    response = requests.request("POST", url, headers=headers, data=payload)
    print(response.text)

    in most of my actual work, I stay away from getting a OTCSTicket and get it from OTDS as it is better when it encounters multiple servers.Also I put my servers in https so that is the only change

  • Hi @appuq ,

    Thanks for the reply. i was able to get the token with slight modifications in my code.