question

Andrew Schwartz avatar image
Andrew Schwartz asked ·

OAuth with Django: 'bytes' object has no attribute 'get'

I'm working through the Authorization Flow Quick Start App using Python 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 slightly modified code that handles the test page:


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


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

oauth2django
1 |1000 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.

Phong Vu avatar image
Phong Vu answered ·

Looks like it crashed at this line in Django

api = request.GET.get('api')

Can you try this

api = request.GET['api']


2 comments
1 |1000 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.

I tested your solution, and the same error is raised. Using request.GET.get('api') should yield the same result as request.GET['api'], unless the key 'api' isn't present, then the original avoids a MultiValueDictKeyError.


I'm pretty confident that the error is triggered by the line:

return resp.response()._content

._content is a bytes object, which fits with the 'bytes object has no attribute get' error. I'm confused why this line is causing problems though because it is using resp, which is returned from RingCentral's own rcsdk.platform object.

I've tried decoding ._content, but that turns it into a string and raises an AttributeError: 'str' has no attribute 'get'

I've tried json.loads, but that turns it into a dict and raises an AttributeError: 'dict' has no attribute 'status_code'

0 Likes 0 ·

Well, you can add some print () before and after a few places. Also the quick start sample code does not handle any exceptions. Add try/except to the code would help to deal with such problem. Maybe the API call fails and the response is not present. Try and let me know

0 Likes 0 ·
Andrew Schwartz avatar image
Andrew Schwartz answered ·

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

1 |1000 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.

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.

Work together.
From anywhere.

Team messaging, video meetings
and phone - all in one app.


Get the new RingCentral app