本文介绍了django_openid_auth TypeError openid.yadis.manager.YadisServiceManager对象不是JSON可序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I used django_openid_auth on my project and it worked quite alright for some time.But today, I tested the application and came across this exception:

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 这里:

I'm not sure what other info should I provide, but I do have my auth.py here:

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作为我的网站。
可以任何人建议?

Then again, upon request for logging in, the exception just pops out. And if that helps, the only thing changed is I started using django 1.6 for my site.could anyone advise?

推荐答案

这似乎是一个已知的错误:

This seems to be a known bug: https://bugs.launchpad.net/django-openid-auth/+bug/1252826

但是,截至目前,似乎没有修复。

However, as of now, there doesn't seem to be a fix.

解决方法,根据,是:

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

这篇关于django_openid_auth TypeError openid.yadis.manager.YadisServiceManager对象不是JSON可序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 12:30