question

ahmer a avatar image
ahmer a asked ahmer a commented

Not getting auth token when using python 3

Hi, all.

I am trying to get auth token using password mechanism using python 3. Earlier I did it with python 2 and it is still working fine. I used urllib.urlencode() method to URL encode the request parameters,as required in the API doc. Equivalent of that method in python 3 is urllib.parse.urlencode() but i'm getting below error when executing in python 3.

{"error": "invalid_request", "errors": [{"errorCode": "OAU-156", "message": "Basic authentication header is missing or malformed"}], "error_description": "Basic authentication header is missing or malformed"}

I even printed the resulting string from urlencode method in both cases and it is same. Can't understand what's the problem here? Any insights?

python 3 code:

    import urllib.parse
    import json
    import requests
    basic="%s:%s" % ("<my cllient id>","<my cllient secret>")
    auth_header = {
        "Content-Type": "application/x-www-form-urlencoded",
        "Accept": "application/json",
        "Authorization": "Basic "+ str(base64.b64encode(basic.encode()))
    }
    body = urllib.parse.urlencode({
        'grant_type': 'password',
        'username': "<my number>",
        'password': "<my password>"
    })
    auth_request=requests.request("POST","https://platform.devtest.ringcentral.com/restapi/oauth/token",headers=auth_header,data=body)
    print(json.dumps(auth_request.json()))

python 2 code(working):

    import urllib
    import json
    import requests
    basic="%s:%s" % ("<my cllient id>","<my cllient secret>")
    auth_header = {
        "Content-Type": "application/x-www-form-urlencoded",
        "Accept": "application/json",
        "Authorization": "Basic "+ str(base64.b64encode(basic.encode()))
    }
    body = urllib.urlencode({
        'grant_type': 'password',
        'username': "<my number>",
        'password': "<my password>"
    })
    auth_request=requests.request("POST","https://platform.devtest.ringcentral.com/restapi/oauth/token",headers=auth_header,data=body)
    print(json.dumps(auth_request.json()))


rest apiauthentication
1 |3000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

1 Answer

Phong Vu avatar image
Phong Vu answered ahmer a commented

You did not post your code so I cannot say what could be wrong. Check out this tutorials to see how to authenticate with password flow using Python 2/3

https://ringcentral-tutorials.github.io/call-ringcentral-apis-native-python-demo/?distinctId=171e1c5b614e8-078e90c97b438a-1d346655-fa000-171e1c5b615fa

1 comment
1 |3000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

ahmer a avatar image ahmer a commented ·

added the code

0 Likes 0 ·

Developer sandbox tools

Using the RingCentral Phone for Desktop, you can dial or receive test calls, send and receive test SMS or Fax messages in your sandbox environment.

Download RingCentral Phone for Desktop:

Tip: switch to the "sandbox mode" before logging in the app:

  • On MacOS: press "fn + command + f2" keys
  • On Windows: press "Ctrl + F2" keys