question

ahmer a avatar image
ahmer a asked ·

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


api auth
1 |1500 characters needed characters left characters exceeded

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

1 Answer

· Write an Answer
Phong Vu avatar image
Phong Vu answered ·

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 |1500 characters needed characters left characters exceeded

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

added the code

0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

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

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