I have been using the REST API for a while in commander 4.x and never had second thoughts about authentication when running within a step (i.e. COMMANDER_SESSIONID) ... until we moved to ecflow. The "client code" we have uses a mixture of old XML-RPC API and REST. The XML-RPC API works fine with the commander session ID exported as an environment variable, but any access to the REST server results in a 401 error.
I looked at the documentation and searched the forum but could not find any useful information on how the session ID should be handled with the REST API and how the request should be constructed in order to work.
Our client code is written in python and we use the requests package. A snippet of code would be really useful.
Answer by nitinp · Aug 23, 2017 at 10:09 PM
Answer by laurent · Aug 23, 2017 at 10:09 PM
Okay, after debugging this a little more, here is what I found (using python). This was in fact the solution I had originally implemented but I had forgotten to reset the expiry date on the cookie, causing this to stop working after a while :(. Hopefully this is useful to others.
import time import requests from cookielib import Cookie # Disable insecure warnings - our server doesn't have root certs from requests.packages.urllib3.exceptions import InsecureRequestWarning requests.packages.urllib3.disable_warnings(InsecureRequestWarning) user = "foo" password="bar" COMMANDER_SERVER = "commander.mycompany.net" session_with_cred = requests.Session() # This session is used to obtain a session ID session_existing_session_id = requests.Session() # This session will set a cookie and re-use the session # ID from the session above # We Authenticate with one session to get a session ID. Normally you would read the COMMANDER_SESSIONID env var. COMMANDER_SESSIONID = session_with_cred.post('https://{}:8443/rest/v1.0/sessions'.format(COMMANDER_SERVER), auth=(user, password), verify=False).json().get('sessionId') print("EC SESSION ID: {}".format(COMMANDER_SESSIONID)) c = Cookie(version=0, name='sessionId', value=COMMANDER_SESSIONID, port=None, port_specified=False, domain=COMMANDER_SERVER, domain_specified=False, domain_initial_dot=False, path='/rest/v1.0', path_specified=False, secure=False, expires=int(time.time()) + 86400, # Set expiry time 24 hours from now discard=False, comment=None, comment_url=None, rest={}, rfc2109=False) session_existing_session_id.cookies.set_cookie(c) response = session_existing_session_id.get('https://{}:8443/rest/v1.0/server/versions'.format(COMMANDER_SERVER), verify=False) print(response.status_code) print(response.json())
Answer by gregm · Feb 08 at 09:00 PM
REST API: How to use SessionID in other HTTP requests? 0 Answers
how to integrate rest api and web login 1 Answer
Alternate Authentication Methods for REST API? 0 Answers
ElectricCommander: determining a job step's authentication principals? 1 Answer
How can I get a specific project's job status using REST API in ElectricFlow? 2 Answers
Electric Cloud powers Continuous Delivery, helping organizations developing deliver better software faster by automating and accelerating build, test, and deployment processes at scale. Industry leaders like Qualcomm, SpaceX, Cisco, GE, Gap, and E*TRADE use Electric Cloud solutions to boost DevOps productivity and Agile throughput.