Back to snippets
genesys_cloud_client_credentials_auth_and_user_retrieval.py
pythonAuthenticates with Genesys Cloud using Client Credentials and
Agent Votes
1
0
100% positive
genesys_cloud_client_credentials_auth_and_user_retrieval.py
1import PureCloudPlatformClientV2
2from PureCloudPlatformClientV2.rest import ApiException
3
4# Replace with your own Client ID, Client Secret, and Environment (e.g., mypurecloud.com)
5CLIENT_ID = 'YOUR_CLIENT_ID'
6CLIENT_SECRET = 'YOUR_CLIENT_SECRET'
7ENVIRONMENT = 'mypurecloud.com'
8
9# Set the environment
10PureCloudPlatformClientV2.configuration.host = f'https://api.{ENVIRONMENT}'
11
12# Instantiate a client authorization library
13api_client = PureCloudPlatformClientV2.api_client.ApiClient()
14
15# Authenticate using Client Credentials Grant
16try:
17 auth_token_info = api_client.get_client_credentials_token(CLIENT_ID, CLIENT_SECRET)
18 print("Authentication successful!")
19except ApiException as e:
20 print(f"Exception when calling ApiClient->get_client_credentials_token: {e}")
21 exit()
22
23# Configure the SDK to use the authenticated token
24PureCloudPlatformClientV2.configuration.access_token = auth_token_info.access_token
25
26# Use the UsersApi to get details about the authenticated user (Client Credentials context)
27users_api = PureCloudPlatformClientV2.UsersApi()
28
29try:
30 # This call retrieves the 'me' user profile for the current token
31 response = users_api.get_users_me()
32 print(f"Hello, my name is {response.name}")
33except ApiException as e:
34 print(f"Exception when calling UsersApi->get_users_me: {e}")