Access token request not returning token?

Hi,

I'm trying to get an access token via POST by following https://developer.inindca.com/api/tutorials/oauth-client-credentials/?language=python&step=1

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.

Hi Robin,

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")

Hi John,

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.

In my case flipping
url = https://login.mypurecloud.com/oauth/token

to

url = https://login.mypurecloud.de/oauth/token

did the trick.

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.

Thanks for the help in any case.

Best,
Robin

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).

Regards,

Hi Robin,

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.

Hi John, Jerome,

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.

Best,
Robin

Hi Robin,

Not a worry. Thanks for following up as it avoided unnecessary research.

Thanks,
    John

Glad you found the issue.

For info, there is also a Platform SDK for Python which can help.
https://developer.mypurecloud.com/api/rest/client-libraries/python/index.html

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.

Just make sure to set the environment (for login.mypurecloud.de). The tutorials are not always showing it in Python.
Do this before calling the get_client_credentials_token method.
https://developer.mypurecloud.com/api/rest/client-libraries/python/index.html#setting_the_environment

I'll go ahead and try using the SDK instead, suspect it may save me some headaches along the way. Thanks for the heads up.

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.