Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

the JSON object must be str, not 'bytes' #491

Open
procmail opened this issue Oct 15, 2014 · 10 comments · May be fixed by #493
Open

the JSON object must be str, not 'bytes' #491

procmail opened this issue Oct 15, 2014 · 10 comments · May be fixed by #493

Comments

@procmail
Copy link

When trying to log in with Facebook on /facebook/example/ , this is what I get:

TypeError at /facebook/connect/
the JSON object must be str, not 'bytes'
Request Method: POST
Request URL: http://www.example.com/facebook/connect/
Django Version: 1.7
Exception Type: TypeError
Exception Value:
the JSON object must be str, not 'bytes'
Exception Location: /usr/local/lib/python3.4/json/init.py in loads, line 312
Python Executable: /usr/home/eric/virtualenvs/djangovirt/bin/python
Python Version: 3.4.1
Python Path:
['/usr/home/eric/virtualenvs/djangovirt/djangoproj',
'/usr/local/lib/python34.zip',
'/usr/local/lib/python3.4',
'/usr/local/lib/python3.4/plat-freebsd10',
'/usr/local/lib/python3.4/lib-dynload',
'/usr/home/eric/virtualenvs/djangovirt/lib/python3.4/site-packages',
'/usr/home/eric/virtualenvs/djangovirt']
Server time: Wed, 15 Oct 2014 16:09:34 +0800

@tschellenbach
Copy link
Owner

I didn't test with Django 1.7 and python 3.4 just yet.
A pull request would be much appreciated.

@stianpr
Copy link

stianpr commented Oct 16, 2014

I am having the same problem when using FacebookAuthorization.parse_signed_data(). I narrowed it down to the decoded payload, on line 475 in api.py, not being decoded to string.

@jonasspott
Copy link

Really loving this package @tschellenbach!
Unfortunately I'm using Python 3.4 with Django 1.7 as well - would you have an idea when you'll be able to make a fix for this? Unfortunately I'm not a good enough programmer to fix it myself, but are willing to wait to build this feature until it is stable for my setup.
Hope to see an update soon :)

@stianpr
Copy link

stianpr commented Oct 21, 2014

I'm working on a fix here: 7d1219b . Though not finalised yet so do not use it. It would be very nice if people would test this fix =) I'm working on the tests now and I will release potential fixes for those ASAP.

@tschellenbach
Copy link
Owner

Great guys, much appreciated!

@stianpr
Copy link

stianpr commented Oct 21, 2014

@tschellenbach I'm having a hard time to test this change. How can I run all tests locally and those specifically for open_facebook?

I want to make sure this fix doesn't break anything in python 2, because all tests with python 2 should pass right? An official python 3 release is not out there yet, if I'm correct , so this is just a bonus for those using django-facebook in python 3 environment IMO.

@tschellenbach
Copy link
Owner

the current version is python 3 compatible in terms of test coverage. but not everything is tested

@tschellenbach
Copy link
Owner

have a look at how the travis.yml file runs the tests

stianpr added a commit to mailmojo/Django-facebook that referenced this issue Oct 22, 2014
`json.loads` was expecting a string, but in python 3 `base64decode()`
return bytes and that is why it bugged. We fix this by making sure
the decoded payload data is in string and that `hmac.new()` is
provided with arguments in bytes. `open_facebook.utils.smart_str`
will do that job correctly in python 2 and 3.

We also use `hmac.compare_digest()` which is the preferred way to compare
those kinds of data to prevent timing analysis. Read more about this on
python docs. Though this is only available from python 2.7.7.

Fixes tschellenbach#491.
stianpr added a commit to mailmojo/Django-facebook that referenced this issue Nov 10, 2014
`json.loads` was expecting a string, but in python 3 `base64decode()`
return bytes and that is why it bugged. We fix this by making sure
the decoded payload data is in string and that `hmac.new()` is
provided with arguments in bytes. `open_facebook.utils.smart_str`
will do that job correctly in python 2 and 3.

We also use `hmac.compare_digest()` which is the preferred way to
compare those kinds of data to prevent timing analysis. If not
`hmac.compare_digest` is available (python 2.7.7+) then we just compare
logically.

Fixes tschellenbach#491.
stianpr added a commit to mailmojo/Django-facebook that referenced this issue Jan 27, 2015
`json.loads` was expecting a string, but in python 3 `base64decode()`
return bytes and that is why it bugged. We fix this by making sure
the decoded payload data is in string and that `hmac.new()` is
provided with arguments in bytes. `open_facebook.utils.smart_str`
will do that job correctly in python 2 and 3.

We also use `hmac.compare_digest()` which is the preferred way to
compare those kinds of data to prevent timing analysis. If not
`hmac.compare_digest` is available (python 2.7.7+) then we just compare
logically.

Fixes tschellenbach#491.
@pterofractal
Copy link

The snippet [https://github.com/tschellenbach/Django-facebook/commit/7d1219b36ef9318495032d8dd0626d11b45fbf9a] looks to work for me. I'm running python 3.4.3

PiDelport added a commit to PiDelport/Django-facebook that referenced this issue Mar 26, 2015
@axeliodiaz
Copy link

I have the same problem... i'm using django 1.8.5 and python 3.4.2, but when i go to /facebook/connect/ my project crash... this is my traceback:

Traceback:
File "/home/axelio/public_html/virtualenvs/project/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
132. response = wrapped_callback(request, _callback_args, *_callback_kwargs)
File "/home/axelio/public_html/virtualenvs/project/lib/python3.4/site-packages/django/views/decorators/csrf.py" in wrapped_view
58. return view_func(_args, *_kwargs)
File "/home/axelio/public_html/virtualenvs/project/lib/python3.4/site-packages/django_facebook/decorators.py" in wrapped_view
99. response = self.authenticate(self.fn, request, _args, *_kwargs)
File "/home/axelio/public_html/virtualenvs/project/lib/python3.4/site-packages/django_facebook/decorators.py" in authenticate
175. request, redirect_uri=redirect_uri)
File "/home/axelio/public_html/virtualenvs/project/lib/python3.4/site-packages/django_facebook/api.py" in require_persistent_graph
28. graph = get_persistent_graph(request, _args, *_kwargs)
File "/home/axelio/public_html/virtualenvs/project/lib/python3.4/site-packages/django_facebook/api.py" in get_persistent_graph
83. graph = get_facebook_graph(request, _args, *_kwargs)
File "/home/axelio/public_html/virtualenvs/project/lib/python3.4/site-packages/django_facebook/api.py" in get_facebook_graph
155. signed_data = parse_signed_request(signed_request_string)
File "/home/axelio/public_html/virtualenvs/project/lib/python3.4/site-packages/django_facebook/utils.py" in parse_signed_request
164. signed_request_string)
File "/home/axelio/public_html/virtualenvs/project/lib/python3.4/site-packages/open_facebook/api.py" in parse_signed_data
475. data = json.loads(base64_url_decode_php_style(payload))
File "/usr/lib/python3.4/json/init.py" in loads
312. s.class.name))

Exception Type: TypeError at /facebook/connect/
Exception Value: the JSON object must be str, not 'bytes'

nickpack pushed a commit to nickpack/Django-facebook that referenced this issue Nov 22, 2018
`json.loads` was expecting a string, but in python 3 `base64decode()`
return bytes and that is why it bugged. We fix this by making sure
the decoded payload data is in string and that `hmac.new()` is
provided with arguments in bytes. `open_facebook.utils.smart_str`
will do that job correctly in python 2 and 3.

We also use `hmac.compare_digest()` which is the preferred way to
compare those kinds of data to prevent timing analysis. If not
`hmac.compare_digest` is available (python 2.7.7+) then we just compare
logically.

Fixes tschellenbach#491.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants