udacity / ud330

105 stars 745 forks source link

oauth2client.client.OAuth2Credentials object is not JSON serializable #4

Closed cristianmtr closed 9 years ago

cristianmtr commented 9 years ago

In lesson 2, step 5:

After I accept the terms for the application to connect to my g+ account, I get the following error in the terminal. The browser does not redirect. I have also tried to run the same test with code supplied here on github, even from future steps in this lesson.

#!python

cristian step7 python project.py 
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 *127.0.0.1 - - [22/Apr/2015 22:37:56] "GET /login HTTP/1.1" 200 -
 *done!
 *127.0.0.1 - - [22/Apr/2015 22:38:00] "POST /gconnect? *state=LEMD4F2L1QNR0KKJMRI51KWWM4HHHQTH HTTP/1.1" 500 -
 *Traceback (most recent call last):
  * File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1836, in __call__
   *  return self.wsgi_app(environ, start_response)
   *File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1820, in wsgi_app
    * response = self.make_response(self.handle_exception(e))
   *File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1479, in full_dispatch_request
    response = self.process_response(response)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1693, in process_response
    self.save_session(ctx.session, response)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 837, in save_session
    return self.session_interface.save_session(self, session, response)
  File "/usr/local/lib/python2.7/dist-packages/flask/sessions.py", line 326, in save_session
    val = self.get_signing_serializer(app).dumps(dict(session))
  File "/usr/local/lib/python2.7/dist-packages/itsdangerous.py", line 565, in dumps
    payload = want_bytes(self.dump_payload(obj))
  File "/usr/local/lib/python2.7/dist-packages/itsdangerous.py", line 847, in dump_payload
    json = super(URLSafeSerializerMixin, self).dump_payload(obj)
  File "/usr/local/lib/python2.7/dist-packages/itsdangerous.py", line 550, in dump_payload
    return want_bytes(self.serializer.dumps(obj))
  File "/usr/local/lib/python2.7/dist-packages/flask/sessions.py", line 85, in dumps
    return json.dumps(_tag(value), separators=(',', ':'))
  File "/usr/local/lib/python2.7/dist-packages/flask/json.py", line 126, in dumps
    rv = _json.dumps(obj, **kwargs)
  File "/usr/lib/python2.7/json/__init__.py", line 250, in dumps
    sort_keys=sort_keys, **kw).encode(obj)
  File "/usr/lib/python2.7/json/encoder.py", line 209, in encode
    chunks = list(chunks)
  File "/usr/lib/python2.7/json/encoder.py", line 434, in _iterencode
    for chunk in _iterencode_dict(o, _current_indent_level):
  File "/usr/lib/python2.7/json/encoder.py", line 408, in _iterencode_dict
    for chunk in chunks:
  File "/usr/lib/python2.7/json/encoder.py", line 442, in _iterencode
    o = _default(o)
  File "/usr/local/lib/python2.7/dist-packages/flask/json.py", line 83, in default
    return _json.JSONEncoder.default(self, o)
  File "/usr/lib/python2.7/json/encoder.py", line 184, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: <oauth2client.client.OAuth2Credentials object at 0x7fca10795b10> is not JSON serializable
linusdong commented 9 years ago

http://discussions.udacity.com/t/oauth-course-google-sign-in-doesnt-work/15444

Try the link above.

Regards, Linus

lobrown commented 9 years ago

@cristianmtr, did downgrading werkzeug solve your issue? You can do this by typing the following three commands inside your vagrant environment:

pip install werkzeug==0.8.3 pip install flask==0.9 pip install Flask-Login==0.1.3

artofkot commented 9 years ago

Is there a way to solve this issue without downgrading werkzeug? Thanks in advance!

linusdong commented 9 years ago
from oauth2client.client import AccessTokenCredentials

# store only the access_token
login_session['credentials'] = credentials.access_token
# return credential object
credentials = AccessTokenCredentials(login_session['credentials'], 'user-agent-value')
ptaferner commented 9 years ago

Just had the same problem, but the solution from Linus works perfectly - thanks man! :-)

ave-llan commented 9 years ago

I was also having this problem and finally fixed it by changing line 100 from login_session['credentials'] = credentials to login_session['credentials'] = credentials.access_token

I can't access the thread on the discussion form that Linus posted, presumably because I'm in a later cohort. Is there any information there about this that I should know? I'm not sure if my change will cause problems later on.

Sesshoumaru404 commented 9 years ago

I using the method @jrleszcz suggested and it seems to have fixed the problem. Thanks

Sesshoumaru404 commented 9 years ago

just remember if you use this approach in the disconnect url code it sets credentials = login_session.get('credentials') a few line below that you should change access_token = credentials.access_token to access_token = credentials

ghost commented 9 years ago

thanks @jrleszcz and @Sesshoumaru404

PrakashSeven commented 9 years ago

Thanks @jrleszcz ,your comment helped me..:)

tylermorganme commented 8 years ago

I ran into this issue during P5. lobrowns solution of downgrading the right packages worked great, but I think I had to reset my server before it took effect.

sudo service apache2 restart
ichiroyamato commented 5 years ago

@linusdong you saved my life. Please don't forget to contact me if you ever have chance to visit Tokyo. I will introduce you to the best sightseeing places.

arminhimself commented 5 years ago

very helpful, thank you.