Legacy Dev Forum Posts

 View Only

Sign Up

Access token request not returning token?

  • 1.  Access token request not returning token?

    Posted 06-05-2025 18:18

    Robin_Nydestedt | 2020-09-28 14:05:53 UTC | #1

    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.


    John_Carnell | 2020-09-28 12:24:58 UTC | #2

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

    Robin_Nydestedt | 2020-09-28 13:09:53 UTC | #3

    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 clientid / clientsecret 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


    Jerome.Saint-Marc | 2020-09-28 13:39:31 UTC | #4

    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,


    John_Carnell | 2020-09-28 13:41:13 UTC | #5

    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.


    Robin_Nydestedt | 2020-09-28 14:04:49 UTC | #6

    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


    John_Carnell | 2020-09-28 14:09:56 UTC | #7

    Hi Robin,

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

    Thanks, John


    Jerome.Saint-Marc | 2020-09-28 14:32:35 UTC | #8

    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 getclientcredentialstoken method. https://developer.mypurecloud.com/api/rest/client-libraries/python/index.html#settingthe_environment


    Robin_Nydestedt | 2020-09-28 14:33:45 UTC | #9

    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.


    system | 2020-10-29 14:33:46 UTC | #10

    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: 8923