News & Announcements User Community Developer Community

Welcome to the RingCentral Community

Please note the community is currently under maintenance and is read-only.

Search
Make sure to review our Terms of Use and Community Guidelines.
  Please note the community is currently under maintenance and is read-only.
Home » Developers
Not getting auth token when using python 3
Tags: rest api, authentication
May 5, 2020 at 1:52pm   •   1 replies  •  0 likes
ahmer a

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 Answer
answered on May 6, 2020 at 7:38am  

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


 0



A new Community is coming to RingCentral!

Posts are currently read-only as we transition into our new platform.

We thank you for your patience
during this downtime.

Try Workflow Builder

Did you know you can easily automate tasks like responding to SMS, team messages, and more? Plus it's included with RingCentral Video and RingEX plans!

Try RingCentral Workflow Builder

PRODUCTS
RingEX
Message
Video
Phone
OPEN ECOSYSTEM
Developer Platform
APIs
Integrated Apps
App Gallery
Developer support
Games and rewards

RESOURCES
Resource center
Blog
Product Releases
Accessibility
QUICK LINKS
App Download
RingCentral App login
Admin Portal Login
Contact Sales
© 1999-2024 RingCentral, Inc. All rights reserved. Legal Privacy Notice Site Map Contact Us