The Cisco Webex API documentation in conjunction with the wxc_sdk documentation
allows you to implement business logic in a programmatic fashion.
Now you will essentially embed your code, along with some error handling,
into the Flask application framework provided.
In your portal, you will perform the following steps:
Add your Service App Client ID, Secret, and Refresh Token to the Environment.
Look up the user by email, get their user object ID and, with that, look up the user's details,
including all Webex Calling information.
Retrieve all licenses, then find the object ID of a Webex Calling and On-prem UCM licenses.
Webex Calling users must be associated with a Location, so the object ID of a desired location must
be retrieved.
Update the user, modifying the licenses and updating their location.
Step 1 - Add your Service App Client ID, Secret, and Refresh Token to the Environment
In the example you completed earlier, you obtained an access token using a Service App client ID,
client secret, and refresh token. This enabled administrative access to Webex.
In this section, you will use the information you obtained previously to enable your Portal to perform administrative
tasks in Webex as well.
Now you can start using the wxc_sdk in your project.
In your VS Code
tab, open up flaskr/api/v1/wbxc.py using the Explorer.
In the earlier examples, you ran the service_app.py script to create an access token,
which you then manually pasted into the wxc_enable_user.py file.
The access token was then used to instantiate a wxc_sdk instance to be able to access the Webex APIs.
To handle this automatically, we have created a class:
In this code, self.get_tokens() will either read a set of access/refresh tokens from a file, or use the
environment variables you set above. If an access token is about to expire or isn't found to begin with,
then the refresh token will be used to create a new access token, as you did in the example. The access
token is then used to create an instance of the API.
By placing this token refresh capability in this class, each time a new instance of this ServiceApp class
is instantiated, the token may be refreshed, if needed, as long as the refresh token has not expired.
To create an instance of the ServiceApp class, you just need this single line of code:
Then to execute queries instead of using the api variable which represents the WebexSimpleApi class like this:
you can use do the following:
Note the use of sa.api.people.list() instead of
api.people.list().
Now you are ready to add to the portal code.
Remove the pass from the section in get_person_det and replace with the following:
The wbxc.py file already contains a class named wbxc_user_api that will be called when the
/wbxc/user/<userid> API is called on your Portal back-end. Extend it to
report success or failure based on the get_person_det() results. Any
RestError exceptions that may be generated by the wxc_sdk are already handled.
Step 3 - Enabling a User for Webex Calling
This section will add the code to enable a user for Webex Calling.
Find the class wbxc_user_enable_api() section in the flaskr/api/v1/wbxc.py file. You
will be updating the put method.
The put method already has code to read in the arguments passed in the HTTP REST request to Flask. In this case the
argument is the userid field. The first step to to enabling a user for Webex Calling is to get the detailed user
information by calling
get_person_det(), then look up the Webex Pro and
On-Prem UCM licenses. Since this is also a common task, a get_lic_by_name() function was
created to look up the respective license in the same way you did in the earlier example sections.
Next, query the Location to which this user and number will be assigned. Following this, look
up the user's phone number, which should already be pre-populated based on what was synchronized
to the account from the Active Directory via the Directory Connector. If no number is found
for the user, return an error. This is an area that could require a lot of
custom enhancements. For example, you could add and/or activate numbers in Webex Calling as they
must first exist there before they can be assigned to a user.
Add the following to your code:
Check to see if the user is already enabled for Webex Calling and if they are not, prepare the user
by adding the Webex Pro license and removing the On-prem UCM license, then set
the location and update the person.
Add the following to your code:
Save this file
In VS Code, start (or restart) your Flask app by clicking
Debug > Start Debugging (or
Restart Debugging, if already started).
The put method in the wbxc_user_disable_api class has already beeen implemented for you. This
function is used to disable Webex Calling and just does the opposite of the steps you just perfromed.
The function looks up the user details and the license, exactly as before, then removes the Webex Calling
License and adds the Unified Communication Manager (UCM) license. The only other difference is that since removing
the Webex Calling license also removes the phone number, you can simply save that data before
updating the user's license and then running another update to the user afterwards to re-apply the
phone number. You can look at the code in the wbxc_user_disable_api medthod if you want
to see the exact code that does this.
Click the GET request with the URI
"/wbxc/user/{userid} Retrieve a user in WbxC by user id"
Click Try it out at the right
In the userid field, enter the userid
pod6wbxuser
Click Execute
You should see a 200 response code and a successful message, with the entire user
object being printed out in the Response body.
In a previous section of this lab, you enabled Webex Calling features for the user pod6wbxuser. To test
the code you just implemented, you will first disable Webex Calling for this user, then re-enable it.
While still in the wbxc section of your
Portal API page,
click the PUT request with the URI "/wbxc/user/disable/{userid}"
and description "Disable a Webex Calling Person by user ID."
Click Try it out at the right
In the userid field, enter the userid
pod6wbxuser
Click Execute
You should see a 200 response code and a successful message, with a message
indicating Successfully disabled Webex Calling for user pod6wbxuser.
The Webex app should be already running on your PC. Sign out by clicking on the
logged in user's profile and settings on the top left of the Webex Client application
window.
Log in using email:
pod6wbxuser@collab-api.com
and password:
C1sco.123
You will be able to log in, however, there should not be phone services available because you deleted the CSF device for this user
in a previous section.
Now go through the steps to re-enable Webex Calling on the phone using the API you created.
Remain in the wbxc section of your Portal API page
and find the PUT request with the URI
"/wbxc/user/enable/{userid}" and description "Enable a Person for Webex Calling by user ID".
Click Try it out at the right
In the userid field, enter the userid
pod6wbxuser
Click Execute
You should see a 200 response code and a successful message, with the entire user
object being printed out in the Response body.
As a final test, sign out of your Webex App, then sign in again with
pod6wbxuser@collab-api.com
and password:
C1sco.123
. You must sign out and sign back in
for the call to work.
Once signed in, you should see the Call Settings in the bottom left corner. There should be no
"Phone services are disconnected" or similar errors.
In the Webex app, click the PU icon at top left. Click Settings,
then click Phone Service. Make sure it shows Webex Calling
and connected.
Click the following to place a call: +19197061111.
This calls another UCM-registered phone via a CUBE Local gateway between Cisco Webex and your
pod's Unified CM.
Webex Calling has a vast amount of configuration exposed via the API. From placing calls to configuring
call handling, many things can be automated and simplified depending on your requirements. In the
next section, you will take a look at using a Bot to send notification, as well as receive commands
that can execute some of the same APIs that you have already programmed.
Note: You will need to have a valid access token for this to work.