Our current API calls do have sessions expiring. However, upon attempting to re-establish a session, users are not logged in unless they first login to the TSPS GUI. This should not be a requirement to setup an API session. Verify whether after TSPS/TSIM goes down does the application re-authenticate and get a new token or does it continue to use the same token as before? Any non 200 or 204 return code, should re-authenticate and try again. Here is the code being used which requires a new GUI login: def login(self): 'Returns True if success, and False if an issue' # Login server is the presentation server # Re-login if our token is too old # FIXME: Can this be made dynamic, based on auth token? if self.auth_token and (time() - self.token_birthtime) < self.token_max_age: return 'Authenticated', 200 login_url = self.sso_url + '/tsws/api/v10.1/token' headers = {'Content-type': 'application/json'} data = {'username': self.user, 'password': self.password, 'tenantName': self.tenant } try: response = self.session.post(login_url, data=json.dumps(data), headers=headers, verify=self.ssl_verify) except Exception as ex: self.log.error("TrueSight %s connection error: %s", login_url, ex) return str(ex), 500 try: result = response.json() except Exception as ex: message = "Invalid data from TrueSight TSPS login() -- is primary now secondary?" self.log.exception(message, extra=dict(text=response.text, exception=ex)) return message + ' ' + response.text, response.status_code message = result.get('statusMsg', response.text) if response.status_code != 200: self.log.error("TrueSight %s issue (HTTP %s): %s", self.base_url, response.status_code, message) return response.text, response.status_code # Yay! We logged in! self.log.info("Logged into %s successfully: %s", self.base_url, response.text) self.auth_token = result['response']['authToken'] self.headers['Authorization'] = 'authtoken ' + str(self.auth_token) self.token_birthtime = time() return 'Authenticated', 200 def logout(self): logout_url = self.sso_url + '/tsws/api/v10.1/token' headers = { 'authToken': self.headers['Authorization'] } try: self.session.delete(logout_url, headers=headers) except Exception: pass self.auth_token = None self.headers['Authorization'] = '' self.log.info("Logged out") return 'Logged out', 200 |
The following code snippet shows the correct way to use the REST API to get a token and from the login rest endpoint: import requests ''' if _name_ == '_main_': CREATE_EVENT_URL = "/bppmws/api/Event/create" json_event = "[\{\"eventSourceHostName\": \"bppmwsserver2.domain.com\",\"eventSourceIPAddress\": \"123.45.678.152\",\"attributes\": {\"CLASS\": \"EVENT\",\"mc_object_uri\": \"\",\"severity\": \"CRITICAL\",\"msg\": \"AppServer CPU usage:reached threshold=80\",\"mc_smc_alias\": \"Prod application SRV2\",\"mc_smc_id\": \"Model1_10000_S0110\",\"mc_owner\": \"Administrator\",\"mc_priority\": \"PRIORITY_5\"}}]" events.append(json_event)
try: urllib3.disable_warnings() try: except Exception as e: |