Nothing Special   »   [go: up one dir, main page]

Generating Python Webex API Code Using Postman

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Generating Python Webex API code using Postman

Objectives:
Part 1: Launch the DEVASC VM

Part 2: Obtain Personal Access Token from Webex

Part 3: Populating fields in a complete Postman Request

Part 4: Verify the code

Part 5: Writing a Python Script by hand

Required Resources:
 1 PC with operating system of your choice
 Virtual Box or VMWare
 DEVASC Virtual Machine
 Webex, Postman, and VS Code

Instructions

Part 1: Launch the DEVASC VM

Part 2: Obtain Personal Access Token from Webex


Step 1: Open the Chromium Web Browser.

Double-click the Chromium Web Browser icon on the desktop.

Step 2: Browse to the Webex Developer website

a. In the address bar type: https://developer.webex.com/ and press Enter.


b. Click on the Documentation tab, scroll down to the Getting Started page, and click on the Accounts and
Authentication section.
c. Notice that log in is required to get your Personal Access Token. If you don't have an account yet, click
Sign Up then Try Meetings Free and follow the instructions.
d. Log in with your new credentials.
e. Copy your developer access token and place it in a safe place or document, as you'll be using it in future
steps of this activity.

Part 3: Populating fields in a complete Postman Request


Step 1: Open Postman

Double-click the Postman icon on the desktop

Step 2: Get a list of the rooms associated with the user


a. In the main window next to the Launchpad tab, click the plus icon "+" to create an Untitled Request. By
default, this will be a GET request.
b. Click the down arrow next to GET to view the different API operations including GET, POST, and DELETE.
Set the method to GET.
c. Set the URL to https://webexapis.com/v1/rooms

Step 3: Define the request headers

a. Click on the Headers tab and create two key/value pairs:


 Under KEY, enter Authorization and under Value type Bearer {your access token}
 Under KEY, enter Content-Type and under Value type application/json
Note that you must leave a space after Bearer and there are no curly braces in or around the token.

b. Click Send to test that your request works.

Step 4: Use Postman's code Generate Code feature

a. Under the Save button, you should find a Code link - click on it.
b. In the Generate Code Snippets window, click Filter languages and select Python - Requests as the target
language.
c. Click the Copy to Clipboard button on the upper right of the Generate Code Snippets.

Part 4. Verify the code


Step 1: Open Terminal

a. Create a Python virtual environment by typing:


python3 -m venv env
source env/bin/activate

Step 2: Open Visual Studio Code

a. Double-click the Visual Studio Code icon on the desktop.


b. Click File > Open Folder, navigate to the labs/devnet-src folder, and click OK.
c. In EXPLORER pane on the left, click New File and name it as code_from_postman.py
d. Paste the contents of the clipboard from Postman.
e. Save the code.

Step 3: Run the code

a. Click on the Terminal tab and select New Terminal.


b. Enter the following command:
devasc@labvm:~/labs python3 code_from_postman.py
c. You should see a response from the Webex API similar to:

Part 5: Writing a Python Script by hand


Step 1: Open Visual Studio Code and write the python script
a. In EXPLORER pane on the left, add New File and name it as code_by_hand.py
b. Paste in the first three lines below to import the needed Python requests package
import requests
c. Define a few variables which will be used later in the code.
endpoint = 'https://webexapis.com/v1'
resource ='/people'
access_token = 'your_access_token'
Be sure to replace your_access_token with your personal access token.
d. Define the HTTP headers and query parameter. Be sure to replace your_email_here with your
Webex user email.
headers = { 'Content-Type': 'application/json', 'authorization': 'Bearer ' + access_token }
param = '?email=your_email_here'
url = endpoint + resource + param
e. The result of the requests operation will be assigned to a response variable.
response = requests.get(url, headers = headers, verify = False)
f. Print the response body to the console.
print(response.json())
g. Save the script.

Step 2: Run the code

a. Click on the Terminal tab and select New Terminal.


b. Enter the following command:
devasc@labvm:~/labs python3 code_by_hand.py
c. You should get output similar to the following:

Step 3: Make the script readable

a. Add .json() to the end of the response = requests.get(url, headers = headers,


verify = False) line.
response = requests.get( url, headers = headers, verify = False).json()
b. At the end of the file, replace the print( response ) line with these three lines:
person = response[ "items" ][ 0 ]
print( 'Name: ' + person[ 'displayName' ] )
print( 'Email: ' + person[ 'emails' ][ 0] )
c. Save and run the script again. The ouput should be much cleaner and more readable.

You might also like