I get response code 200, but it fails when I try to call .json() on the response. The content seems to be in html and as far as I can tell there is no access token in the response.
Using Python 3.8. Any ideas?
Best,
Robin
EDIT: Solved. Needed to use the correct region URL (.de in my case).
EDIT2: Actual issue pinpointed to calling http:// instead of https://, having the wrong region will otherwise return response 400, but if the url has http:// it returns 200.
I pulled down the same code and ran it with 3.8.5 and was able to successfully dump the JSON payload and see the access token from the initial request out to Auth. Here is the script. Can you validate that you are printing out the right variable in your script.
Thanks,
John Carnell
Manager, Developer Engagement
# Base64 encode the client ID and client secret
authorization = base64.b64encode(bytes(client_id + ":" + client_secret, "ISO-8859-1")).decode("ascii")
# Prepare for POST /oauth/token request
request_headers = {
"Authorization": f"Basic {authorization}",
"Content-Type": "application/x-www-form-urlencoded"
}
request_body = {
"grant_type": "client_credentials"
}
# Get token
response = requests.post("https://login.mypurecloud.com/oauth/token", data=request_body, headers=request_headers)
# Check response
if response.status_code == 200:
print("Got token")
else:
print(f"Failure: { str(response.status_code) } - { response.reason }")
sys.exit(response.status_code)
# Get JSON response body
response_json = response.json()
print(response_json)
# Prepare for GET /api/v2/authorization/roles request
requestHeaders = {
"Authorization": f"{ response_json['token_type'] } { response_json['access_token']}"
}
# Get roles
response = requests.get("https://api.mypurecloud.com/api/v2/authorization/roles", headers=requestHeaders)
# Check response
if response.status_code == 200:
print("Got roles")
else:
print(f"Failure: { str(response.status_code) } - { response.reason }")
sys.exit(response.status_code)
# Print roles
print("\nRoles:")
for entity in response.json()["entities"]:
print(f" { entity['name'] }")
print("\nDone")
Thank you for checking and getting back to me quick. I managed to pinpoint the issue to being caused by the region I'm in, whereas I hadn't realized I needed to change the url.
Seemingly having correct client_id / client_secret for one region and connecting to another causes this, giving a success response where response.text() is a (largely irrelevant as far as I can tell) html rather than the expected json.
This is strange.
I just tried the same sample code (using python 3.8 + requests module), pointing to login.mypurecloud.com while my sandbox is in login.mypurecloud.ie and using ClientId/ClientSecret of my OAuth client which uses Client Credentials Grant, and I get a 400 Bad request as response (which is what is expected).
That would do it. I am going to open a ticket to look at a 200 when the region does not point to the right region. That seems like a bug. Thanks and I hope this did not cause you too many problems.
I have a correction on my part, I tried to recrease the issue and now I got the 400 as expected. Seems I accidentally fixed the actual issue when I altered the URL, I was trying to call a http:// url rather than https:// originally, this is what actually caused the success response that didn't have the json.
The tutorial is correct, my use of it was not. Sorry for the confusion.
The OAuth Client Credentials tutorial is sending raw HTTP requests - which is of course still allowed - to demonstrate how to complete an OAuth flow.
Other tutorials showing python example are making use of the SDK I think.