django_openid_auth TypeError openid.yadis.manager.YadisServiceManager对象不是JSON可序列化的

时间:2013-11-30 14:22:16

标签: python django django-openid-auth

我在我的项目中使用django_openid_auth并且它在一段时间内工作得很好。 但今天,我测试了该应用程序并遇到了这个例外:

Environment:


Request Method: GET
Request URL: http://localhost:7777/google/login/

Django Version: 1.6
Python Version: 2.7.3
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'meet',
 'django_openid_auth',
 'django_jenkins')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.middleware.locale.LocaleMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.common.CommonMiddleware',
 'meet.middlewares.TimezoneMiddleware')


Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
  201.                 response = middleware_method(request, response)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/sessions/middleware.py" in process_response
  38.                     request.session.save()
File "/usr/local/lib/python2.7/dist-packages/django/contrib/sessions/backends/db.py" in save
  57.             session_data=self.encode(self._get_session(no_load=must_create)),
File "/usr/local/lib/python2.7/dist-packages/django/contrib/sessions/backends/base.py" in encode
  87.         serialized = self.serializer().dumps(session_dict)
File "/usr/local/lib/python2.7/dist-packages/django/core/signing.py" in dumps
  88.         return json.dumps(obj, separators=(',', ':')).encode('latin-1')
File "/usr/lib/python2.7/json/__init__.py" in dumps
  238.         **kw).encode(obj)
File "/usr/lib/python2.7/json/encoder.py" in encode
  201.         chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python2.7/json/encoder.py" in iterencode
  264.         return _iterencode(o, 0)
File "/usr/lib/python2.7/json/encoder.py" in default
  178.         raise TypeError(repr(o) + " is not JSON serializable")

Exception Type: TypeError at /google/login/
Exception Value: <openid.yadis.manager.YadisServiceManager object at 0x7fd2f43b2250> is not JSON serializable

我不确定我应该提供哪些其他信息,但我的确有auth.py

from django.contrib.auth.models import User
from openid.consumer.consumer import SUCCESS
from django.core.mail import mail_admins

class GoogleBackend:
    def authenticate(self, openid_response):
        if openid_response is None:
            return None
        if openid_response.status != SUCCESS:
            return None

        google_email = openid_response.getSigned('http://openid.net/srv/ax/1.0',  'value.email')
        google_firstname = openid_response.getSigned('http://openid.net/srv/ax/1.0', 'value.firstname')
        google_lastname = openid_response.getSigned('http://openid.net/srv/ax/1.0', 'value.lastname')
        try:
            #user = User.objects.get(username=google_email)
            # Make sure that the e-mail is unique.
            user = User.objects.get(email=google_email)
            if user.first_name == u'' :
                user.first_name = google_firstname
            if user.last_name == u'' :
                user.last_name = google_lastname

        except User.DoesNotExist:
            user = User.objects.create_user(google_email, google_email, 'password')
            user.first_name = google_firstname
            user.last_name = google_lastname
            user.save()
            user = User.objects.get(username=google_email)
            return user

    return user

    def get_user(self, user_id):

        try:
            return User.objects.get(pk=user_id)
        except User.DoesNotExist:
            return None

以及urls.py包含以下两行:

url(r'^google/login/$', 'django_openid_auth.views.login_begin', name='openid-login'),
url(r'^google/login-complete/$', 'django_openid_auth.views.login_complete', name='openid-complete'),

然后,根据请求登录,异常弹出。如果这有帮助,唯一改变的是我开始在我的网站上使用django 1.6。 有人可以建议吗?

2 个答案:

答案 0 :(得分:11)

这似乎是一个已知的错误:https://bugs.launchpad.net/django-openid-auth/+bug/1252826

然而,截至目前,似乎没有解决方法。

根据this comment,解决方法是to reset the serializer

SESSION_SERIALIZER = 'django.contrib.sessions.serializers.PickleSerializer'

答案 1 :(得分:1)

YEP!我检查了! 正如我所说,这是因为新版django, 我迁回1.5.5并且再次正常工作!