2.3.1.1 Lab - Getting A Service Ticket With Python
2.3.1.1 Lab - Getting A Service Ticket With Python
2.3.1.1 Lab - Getting A Service Ticket With Python
Objectives
Part 1: Get a Service Ticket in Postman
Part 2: Get a Service Ticket in Python
Background / Scenario
Before requests can be made to other APIC-EM API endpoints, a service ticket number must be obtained.
Each request must include a valid service ticket number that serves as an authentication token. In this lab,
you will first use Postman to request a service ticket from the APIC-EM API. You will then duplicate the
process of making the request in Python. Finally, you will convert your program into a reusable function that
will used in later labs.
This request is addressed to the APIC-EM Role Based Access Control API /ticket endpoint.
Required Resources
• Postman
• Python 3 with IDLE
• Python requests module
• Access to the Internet
Note: Use the APIC-EM sandbox URL and credentials provided by your instructor. Only use the public
APIC-EM URL and credentials for additional study after the conclusion of the workshop. For example
purposes, this activity uses the URL and credentials of the public sandbox.
Cisco and/or its affiliates. All rights reserved. Cisco Confidential Page 1 of 8 www.netacad.com
Lab - Getting a Service Ticket with Python
c. Under the General tab, set the SSL certificate verification to OFF. Close the Settings dialog box.
Cisco and/or its affiliates. All rights reserved. Cisco Confidential Page 2 of 8 www.netacad.com
Lab - Getting a Service Ticket with Python
Cisco and/or its affiliates. All rights reserved. Cisco Confidential Page 3 of 8 www.netacad.com
Lab - Getting a Service Ticket with Python
Note: If there is an error, check the Status of the request. Check the meaning of the status code. 200 means
success. A 404 error may mean that the URL was entered incorrectly. A 401 or 403 error could indicate a
problem with the authentication, so check that the credentials are entered correctly in the request body.
Cisco and/or its affiliates. All rights reserved. Cisco Confidential Page 4 of 8 www.netacad.com
Lab - Getting a Service Ticket with Python
a. Open IDLE.
b. Click Options > Configure IDLE > General tab.
c. Change Initial Window Size to Width = 100 and click Ok.
Cisco and/or its affiliates. All rights reserved. Cisco Confidential Page 5 of 8 www.netacad.com
Lab - Getting a Service Ticket with Python
c. Create a dictionary variable named body_json that has two keys needed for authentication, username
and password. Replace the username and password values with the credentials you are assigned for
this workshop.
body_json = {
"username": "!!!REPLACEME with the Username!!!",
"password": "!!!REPLACEME with the Password!!!"
}
Element Explanation
b. Save your script and run it. There will not be any output yet but the script should run without errors. If not,
review the steps and make locate and fix any errors.
Cisco and/or its affiliates. All rights reserved. Cisco Confidential Page 6 of 8 www.netacad.com
Lab - Getting a Service Ticket with Python
d. Save and run your script. You should get output similar to the following although your service ticket
number will be different:
{'response': {'serviceTicket': 'ST-5160-QHyLDw4TnICAzdNs63Vf-cas', 'idleTimeout':
1800, 'sessionTimeout': 21600}, 'version': '1.0'}
e. Create a variable named serviceTicket and assign the value of the serviceTicket key from the response
key. Recall that you must first identify the response key and then you can access the serviceTicket key as
follows:
serviceTicket = response_json["response"]["serviceTicket"]
f. Add a print statement to display the service ticket number, as follows:
print("The service ticket number is: ", serviceTicket)
g. Save and run your script. If you experience errors, check the code again.
Note: If you are stuck, compare your code to the 01_get_ticket_sol.py file that is in the folder with the
workshop files.
Cisco and/or its affiliates. All rights reserved. Cisco Confidential Page 7 of 8 www.netacad.com
Lab - Getting a Service Ticket with Python
f. Click Format > Indent Region, as shown below, or press the TAB key to indent the region.
g. Finally, add a line at the end of the indented code. The service ticket number is returned to the program
that calls the function. Add this line:
return serviceTicket
h. Save and run the my_apic_em_functions.py script. The file will run in the IDLE Shell, although nothing
is displayed. Call your function by entering get_ticket() in the IDLE Shell. You should see output similar
to the following:
>>> get_ticket()
The service ticket number is: ST-5274-Ey2TCHlMVqntwNqEkGzs-cas
'ST-5274-Ey2TCHlMVqntwNqEkGzs-cas'
>>>
i. If you have problems running your code at any time, refer to the appropriate solutions file for help if
necessary.
Cisco and/or its affiliates. All rights reserved. Cisco Confidential Page 8 of 8 www.netacad.com