A comprehensive Flask application demonstrating how to integrate with the Webex Contact Center REST API. This sample showcases agent desktop session management, OAuth authentication, and seamless integration with Webex Contact Center services.
This sample shows how to use the Webex Contact Center API to do the following:
- OAuth Authentication with Webex Contact Center
- Agent Login to desktop sessions programmatically
- Agent Logout from desktop sessions
- Token Management with automatic refresh handling
- Session Management with Flask session storage
- Error Handling with retry logic for expired tokens
- Python 3.6 or higher
- Flask web framework
- Requests library for HTTP calls
- Webex Contact Center Account or Developer Sandbox
- Webex Contact Center Integration with
cjp:userscope - Agent ID and Team ID from your Contact Center configuration
- Dial Number for agent extension
- Request Developer Sandbox: Visit developer.webex-cx.com/sandbox
- Register Integration: Create a Webex Contact Center integration with
cjp:userscope - Learn More: Integration Documentation
- Gather Required Information:
- Client ID and Client Secret
- Agent ID (from Control Hub or WebSocket notifications)
- Team ID (from Control Hub or WebSocket notifications)
- Dial Number for agent
-
Clone the repository:
git clone https://github.com/your-repo/webex-contact-center-integration.git cd webex-contact-center-integration -
Install dependencies:
pip install -r requirements.txt
Or install manually:
pip install flask requests
-
Configure variables in
desktop.py:CLIENT_ID = "YourClientId" CLIENT_SECRET = "YourClientSecret" REDIRECT_URI = "http://0.0.0.0:10060/oauth" AGENT_ID = "YourAgentId" TEAM_ID = "YourTeamId" DIAL_NUMBER = "1001"
-
Add Authorization URL in
templates/index.html:<a href='Your Authorization URL here'>
-
Start the application:
python3 desktop.py
-
Access the application:
Open your browser and navigate to
http://127.0.0.1:10060
webex-contact-center-agent-desktop-api-sample/
βββ desktop.py # Main Flask application
βββ requirements.txt # Python dependencies
βββ static/css/ # CSS styling files
βββ templates/ # HTML templates
β βββ index.html # Main authorization page
β βββ granted.html # Post-authorization page
β βββ agent_loggedin.html # Agent logged in state
β βββ temp.html # Base template
βββ package.json # Development tools configuration
βββ README.md # This file
| Feature | Description | API Endpoint |
|---|---|---|
| OAuth Flow | Authenticate with Webex Contact Center | /v1/access_token |
| Agent Login | Start agent desktop session | /v1/agents/login |
| Agent Logout | End agent desktop session | /v1/agents/logout |
| Token Refresh | Automatically refresh expired tokens | /v1/access_token |
- Authorization: Agent clicks "Connect to WxCC" to start OAuth flow
- Token Exchange: Authorization code exchanged for access and refresh tokens
- Agent Login: Agent clicks "Agent Login" to start desktop session
- Session Active: Agent can handle calls and perform contact center functions
- Agent Logout: Agent clicks "Agent Logout" to end desktop session
The application implements the standard OAuth 2.0 authorization code flow:
# Token exchange
def get_tokens(code):
url = "https://webexapis.com/v1/access_token"
payload = {
"grant_type": "authorization_code",
"client_id": CLIENT_ID,
"client_secret": CLIENT_SECRET,
"code": code,
"redirect_uri": REDIRECT_URI
}
# Exchange code for tokensThe application includes automatic token refresh:
def get_tokens_refresh():
# Refresh expired access tokens using refresh token
payload = {
"grant_type": "refresh_token",
"client_id": CLIENT_ID,
"client_secret": CLIENT_SECRET,
"refresh_token": session['refresh_token']
}@app.route("/agentlogin", methods=['POST'])
def agentLogin():
url = "https://api.wxcc-us1.cisco.com/v1/agents/login"
payload = {
"dialNumber": dialNumber,
"teamId": teamId,
"isExtension": True,
"roles": ["agent"],
"deviceType": "BROWSER"
}
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {access_token}"
}@app.route("/agentlogout", methods=['POST'])
def agentLogout():
url = "https://api.wxcc-us1.cisco.com/v1/agents/logout"
payload = {
"logoutReason": "AGENT_LOGOUT from sample app",
"agentId": agentId
}The application includes comprehensive error handling with automatic retry:
if response.status_code != 202:
if response.status_code == 401:
get_tokens_refresh() # Refresh token and retry
response = requests.request("POST", url, json=payload, headers=headers)The application uses a clean, minimal web interface with:
- Bootstrap-style CSS for professional appearance
- Flask templates with inheritance for maintainable code
- Form-based interactions for agent login/logout
- Responsive design that works on desktop and mobile
- index.html: Main page with "Connect to WxCC" button
- granted.html: Post-authorization page with "Agent Login" button
- agent_loggedin.html: Active session page with "Agent Logout" button
| Variable | Description | Example |
|---|---|---|
CLIENT_ID |
Integration Client ID | C1234567890abcdef... |
CLIENT_SECRET |
Integration Client Secret | secret123... |
REDIRECT_URI |
OAuth redirect URL | http://0.0.0.0:10060/oauth |
AGENT_ID |
Contact Center Agent ID | agent123@example.com |
TEAM_ID |
Contact Center Team ID | team456 |
DIAL_NUMBER |
Agent extension number | 1001 |
You can obtain Agent ID and Team ID through:
- Control Hub: Webex Contact Center administration portal
- WebSocket Notifications: Subscribe to notifications
- API Calls: Use Contact Center APIs to retrieve user and team information
- Environment Variables: Store credentials in environment variables, not source code
- HTTPS: Use HTTPS in production for secure token transmission
- Token Storage: Implement secure token storage (not Flask sessions)
- Rate Limiting: Implement API rate limiting and retry logic
For production deployment:
# Use environment variables
import os
CLIENT_ID = os.getenv('WXCC_CLIENT_ID')
CLIENT_SECRET = os.getenv('WXCC_CLIENT_SECRET')
AGENT_ID = os.getenv('WXCC_AGENT_ID')# Enhanced error handling for production
try:
response = requests.post(url, json=payload, headers=headers, timeout=30)
response.raise_for_status()
except requests.exceptions.RequestException as e:
logger.error(f"API call failed: {e}")
# Implement retry logic or user notification-
Virtual Environment:
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate pip install -r requirements.txt
-
Debug Mode:
if __name__ == '__main__': app.run("0.0.0.0", port=10060, debug=True)
Example: Adding agent state management:
@app.route("/agentstatus", methods=['GET'])
def agentStatus():
url = "https://api.wxcc-us1.cisco.com/v1/agents/status"
headers = {"Authorization": f"Bearer {session['oauth_token']}"}
response = requests.get(url, headers=headers)
return response.json()- Webex Contact Center Developer Portal
- Contact Center API Documentation
- Integration Management
- Developer Sandbox Request
- WebSocket Notifications
- Fork the repository
- Create a feature branch
- Make your changes
- Test thoroughly with Contact Center environment
- Submit a pull request
This project is licensed under the Cisco Sample Code License.
- β Permitted: Copy, modify, and redistribute for use with Cisco products
- β Prohibited: Use independent of Cisco products or to compete with Cisco
- βΉοΈ Warranty: Provided "as is" without warranty
- βΉοΈ Support: Not supported by Cisco TAC
See the LICENSE file for full license terms.
- Create an issue in this repository
- Visit Webex Contact Center Developer Support
- Join the Webex Developer Community
Ready to integrate with Webex Contact Center! π