djangooauth-2.0ringcentral

RingCentral OAuth with Django


I'm working through RingCentral's Authorization Flow Quick Start App using Django. This required making a few changes to the Flask code provided, but most of the flow is working. The index page sends me to RingCentral login, which then sends me back to the test page as it should. But when I click on any of the three links on that page I get the same error:

AttributeError at /test/
'bytes' object has no attribute 'get'

Here's the Django view that handles the test page (slightly modified from the Flask code provided):

def test(request):
    platform = SyncConfig.rcsdk.platform()
    platform.auth().set_data(request.session['sessionAccessToken'])
    if platform.logged_in() == False:
        return index(request)
        api = request.GET.get('api')
    if api == "extension":
        resp = platform.get("/restapi/v1.0/account/~/extension")
        return resp.response()._content
    elif api == "extension-call-log":
        resp = platform.get("/restapi/v1.0/account/~/extension/~/call-log")
        return resp.response()._content
    elif api == "account-call-log":
        resp = platform.get("/restapi/v1.0/account/~/call-log")
        return resp.response()._content
    else:
        return render(request, 'sync/test.html')

And sync/test.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
</head>
<body>
  <b><a href="/logout">Logout</a></b>
  <h2>Call APIs</h2>
  <ul>
      <li><a href="/test?api=extension">Read Extension Info</a></li>
      <li><a href="/test?api=extension-call-log">Read Extension Call Log</a></li>
      <li><a href="/test?api=account-call-log">Read Account Call Log</a></li>
  </ul>
</body>
</html>

Has anyone setup a Django authorization flow for RingCentral and can show me where this is breaking?


Solution

  • I figured it out. Simply returning resp.response()._content from a view was causing Django problems. I wrote a simple view to display the data and returned the rendered template instead, with resp.response()._content included in the context.

    def test(request):
        platform = SyncConfig.rcsdk.platform()
        platform.auth().set_data(request.session['sessionAccessToken'])
        if platform.logged_in() == False:
            return index(request)
        api = request.GET.get('api')
        if api == "extension":
            resp = platform.get("/restapi/v1.0/account/~/extension")
            content = json.loads(resp.response()._content)
            return render(request, 'sync/response.html', {'content': content})
        elif api == "extension-call-log":
            resp = platform.get("/restapi/v1.0/account/~/extension/~/call-log")
            content = json.loads(resp.response()._content)
            return render(request, 'sync/response.html', {'content': content})
        elif api == "account-call-log":
            resp = platform.get("/restapi/v1.0/account/~/call-log")
            content = json.loads(resp.response()._content)
            return render(request, 'sync/response.html', {'content': content})
        else:
            return render(request, 'sync/test.html')
    

    And here's sync/response.html

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8">
    </head>
    <body>
        <b><a href="/logout">Logout</a></b>
        <h2>API Response</h2>
        <ul>
            {% for k, v in content.items %}
                <li>{{ k }}: {{ v }}</li>
            {% endfor %}
        </ul>
    </body>
    </html>