Creating a Public Certificate Request Function Parent topic

The SecureCloud public certificate is openly available to all requests. Along with the digest passphrase and its corresponding broker account name, the public certificate is required to request a SecureCloud user access token.
The following task shows how to request the SecureCloud public certificate, add the public certificate to a variable, and format the public certificate into XML.
Note
Note
Each 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.

Procedure

  1. Define this entire part of the API request as a function.
    In the example, this function is the Get Public Certificate function. The function is defined as get_server_certificate().
    def get_server_certificate():
  2. Create the API URL variable using the CertificateRequest() API.
    api_url = "https://ms.securecloud.com/broker/API.SVC/v3.5/PublicCertificate/"
  3. Copy your digest authentication process and paste it into this part of the API request.
    The digest authentication process parts can be placed at varying locations in the request code, as long as the programming language does not encounter sequencing problems. Declare the API URL variable before calling API URL requests.
  4. Create the API request.
    try:
          sc_get_req = opener.open(req)
          res = sc_get_req.read()
    Note
    Note
    For 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.
  5. Format the public certificate into XML.
    In this example, the variable res contains the unformatted public certificate. The variable certificate is the intended variable to contain the XML format public certificate.
    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)
  6. 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
  7. Perform error checking.
    except urllib2.HTTPError, e:
          logging.error(e)
    except urllib2.URLError, e:
          logging.error(e)
    except Exception, e:
          logging.error(e)

Sample Code Parent topic

# 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)