![]() |
NoteEach of these programming tasks assumes that you are you are using a new
programming code file. The examples shown are using Python programming language. Other
programming languages, such as C++ or Java, are capable of performing the same requests.
|
Get Public Certificatefunction. The function is defined as get_server_certificate().
def get_server_certificate():
api_url = "https://ms.securecloud.com/broker/API.SVC/v3.5/PublicCertificate/"
try: sc_get_req = opener.open(req) res = sc_get_req.read()
![]() |
NoteFor the sake of this example, this section is just the beginning of
the Python compound statement try. If using Python, continue this
try statement until the end of the function.
|
xmldata = xml.dom.minidom.parseString(res) certificate_response = xmldata.getElementsByTagName("certificateResponse")[0] certificate_list = certificate_response.getElementsByTagName("certificateList")[0] certificate_node = certificate_response.getElementsByTagName("certificate")[0] certificate = getNodeText(certificate_node)
certificate = """-----BEGIN RSA PUBLIC KEY-----\n%s\n-----END RSA PUBLIC KEY-----\n""" % (certificate) certificate = str(certificate) return certificate
except urllib2.HTTPError, e: logging.error(e) except urllib2.URLError, e: logging.error(e) except Exception, e: logging.error(e)
# Define this part of the API request as a function. def get_server_certificate(): # Create the API URL variable using the CertificateRequest() API. api_url = "https://ms.securecloud.com/broker/API.SVC/v3.5/PublicCertificate/" # Create variables for each part of the broker account information. digest_broker_account = "test_digest_account" digest_password = "QTpYVL0QKI" digest_realm = "securecloud@trend.com" # Add broker account information and the API URL to the password manager in the same function. pwd_mgr = urllib2.HTTPPasswordMgr() pwd_mgr.add_password(digest_realm, api_url, digest_broker_account, digest_password) # Add the broker account information and the API URL to the API request handler. opener = urllib2.build_opener() opener.add_handler(urllib2.HTTPDigestAuthHandler(pwd_mgr)) # Request the API using the API URL. req = urllib2.Request(api_url) # Set the request data type to XML format. req.add_header('Content-Type', 'application/xml; charset=utf-8') # Include the broker account information in the header. req.add_header('BrokerName', digest_broker_account) # Create the API request. try: sc_get_req = opener.open(req) res = sc_get_req.read() # Format the public certificate into XML. xmldata = xml.dom.minidom.parseString(res) certificate_response = xmldata.getElementsByTagName("certificateResponse")[0] certificate_list = certificate_response.getElementsByTagName("certificateList")[0] certificate_node = certificate_response.getElementsByTagName("certificate")[0] certificate = getNodeText(certificate_node) # Add the public certificate beginning and ending strings. certificate = """-----BEGIN RSA PUBLIC KEY-----\n%s\n-----END RSA PUBLIC KEY-----\n""" % (certificate) certificate = str(certificate) return certificate # Perform error checking. except urllib2.HTTPError, e: logging.error(e) except urllib2.URLError, e: logging.error(e) except Exception, e: logging.error(e)