Skip to main content
Question

Not getting auth token when using python 3

  • 5 May 2020
  • 1 reply
  • 1077 views

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


1 reply

Userlevel 2
Badge

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

Reply