Shubham_Bhosale | 2024-08-22 10:55:24 UTC | #1
import base64, requests, sys, os
print("---------------------------------------------------") print("- Genesys Cloud Python Client Credentials Example -") print("---------------------------------------------------")
CLIENTID = os.environ['GENESYSCLOUDCLIENTID'] CLIENTSECRET = os.environ['GENESYSCLOUDCLIENTSECRET'] ENVIRONMENT = os.environ['GENESYSCLOUDENVIRONMENT'] # eg. mypurecloud.com
Base64 encode the client ID and client secret
authorization = base64.b64encode(bytes(CLIENTID + ":" + CLIENTSECRET, "ISO-8859-1")).decode("ascii")
Prepare for POST /oauth/token request
requestheaders = { "Authorization": f"Basic {authorization}", "Content-Type": "application/x-www-form-urlencoded" } requestbody = { "granttype": "clientcredentials" }
Get token
response = requests.post(f"https://login.{ENVIRONMENT}/oauth/token", data=requestbody, headers=requestheaders)
Check response
if response.statuscode == 200: print("Got token") else: print(f"Failure: { str(response.statuscode) } - { response.reason }") sys.exit(response.status_code)
Get JSON response body
response_json = response.json()
Prepare for GET /api/v2/authorization/roles request
requestHeaders = { "Authorization": f"{ responsejson['tokentype'] } { responsejson['accesstoken']}" }
Get roles
response = requests.get(f"https://api.{ENVIRONMENT}/api/v2/authorization/roles", headers=requestHeaders)
Check response
if response.statuscode == 200: print("Got roles") else: print(f"Failure: { str(response.statuscode) } - { response.reason }") sys.exit(response.status_code)
Print roles
print("\nRoles:") for entity in response.json()["entities"]: print(f" { entity['name'] }")
print("\nDone")
Facing Below Issue
I am using above python code provided by genesys for fetching token but when I run this locally I am able to get the token but when run in production then getting an error - Max retries exceeded with url : /oauth/token (caused by ProxyError ('Cannot connect to proxy.', OSError ('Tunnel Connection failed : 403 Forbidden')))
Declan_ginty | 2024-08-22 11:31:08 UTC | #2
Hi,
I was getting the same error until I realised I had my ENVIRONMENT variable set to us-east-1 but it should have been mypurecloud.com. You can see the full list of URLs for the different regions here.
Regards, Declan
Shubham_Bhosale | 2024-08-22 11:42:10 UTC | #3
Hello,
I am using euw2.pure.cloud in the ENVIRONMENT variable, and according to the list of URLs you provided, euw2.pure.cloud is indeed listed for the eu-west-2 region. However, I am still encountering this error.
Regards, Shubham
Declan_ginty | 2024-08-29 11:14:55 UTC | #4
Hi,
Sorry for the delay. This might be a problem with the login service. Try opening a ticket with care here
Regards, Declan
Shubham_Bhosale | 2024-08-29 11:26:22 UTC | #5
Sure, Thanks
Regards, Shubham
system | 2024-09-29 11:27:10 UTC | #6
This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.
This post was migrated from the old Developer Forum.
ref: 28069