密钥(email)=()已存在错误:python-social-auth

时间:2017-08-29 17:38:38

标签: python django facebook oauth python-social-auth

我的应用使用python-social-auth(0.2.11)同时使用电子邮件注册和Facebook或Google+注册。当 某些 新用户使用Facebook注册时,他们会进入错误页面。之后,我看到以下错误:

Internal Server Error: /complete/facebook/
Traceback (most recent call last):
  File "/app/.heroku/python/lib/python2.7/site-packages/django/core/handlers/base.py", line 132, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/app/.heroku/python/lib/python2.7/site-packages/newrelic/hooks/framework_django.py", line 527, in wrapper
    return wrapped(*args, **kwargs)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/views/decorators/cache.py", line 57, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
    return view_func(*args, **kwargs)
  File "/app/.heroku/python/lib/python2.7/site-packages/social/apps/django_app/utils.py", line 51, in wrapper
    return func(request, backend, *args, **kwargs)
  File "/app/.heroku/python/lib/python2.7/site-packages/social/apps/django_app/views.py", line 28, in complete
    redirect_name=REDIRECT_FIELD_NAME, *args, **kwargs)
  File "/app/.heroku/python/lib/python2.7/site-packages/social/actions.py", line 43, in do_complete
    user = backend.complete(user=user, *args, **kwargs)
  File "/app/.heroku/python/lib/python2.7/site-packages/social/backends/base.py", line 41, in complete
    return self.auth_complete(*args, **kwargs)
  File "/app/.heroku/python/lib/python2.7/site-packages/social/utils.py", line 229, in wrapper
    return func(*args, **kwargs)
  File "/app/.heroku/python/lib/python2.7/site-packages/social/backends/facebook.py", line 87, in auth_complete
    return self.do_auth(access_token, response, *args, **kwargs)
  File "/app/.heroku/python/lib/python2.7/site-packages/social/backends/facebook.py", line 119, in do_auth
    return self.strategy.authenticate(*args, **kwargs)
  File "/app/.heroku/python/lib/python2.7/site-packages/social/strategies/django_strategy.py", line 96, in authenticate
    return authenticate(*args, **kwargs)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/auth/__init__.py", line 74, in authenticate
    user = backend.authenticate(**credentials)
  File "/app/.heroku/python/lib/python2.7/site-packages/social/backends/base.py", line 82, in authenticate
    return self.pipeline(pipeline, *args, **kwargs)
  File "/app/.heroku/python/lib/python2.7/site-packages/social/backends/base.py", line 85, in pipeline
    out = self.run_pipeline(pipeline, pipeline_index, *args, **kwargs)
  File "/app/.heroku/python/lib/python2.7/site-packages/social/backends/base.py", line 112, in run_pipeline
    result = func(*args, **out) or {}
  File "/app/.heroku/python/lib/python2.7/site-packages/social/pipeline/user.py", line 70, in create_user
    'user': strategy.create_user(**fields)
  File "/app/.heroku/python/lib/python2.7/site-packages/social/strategies/base.py", line 53, in create_user
    return self.storage.user.create_user(*args, **kwargs)
  File "/app/.heroku/python/lib/python2.7/site-packages/social/storage/django_orm.py", line 60, in create_user
    return cls.user_model().objects.create_user(*args, **kwargs)
  File "/app/wehealth/users/models.py", line 47, in create_user
    user.save(using=self._db)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/base.py", line 710, in save
    force_update=force_update, update_fields=update_fields)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/base.py", line 738, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/base.py", line 822, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/base.py", line 861, in _do_insert
    using=using, raw=raw)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/manager.py", line 127, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/query.py", line 920, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 963, in execute_sql
    cursor.execute(sql, params)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/utils.py", line 97, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/app/.heroku/python/lib/python2.7/site-packages/newrelic/hooks/database_dbapi2.py", line 25, in execute
    *args, **kwargs)
IntegrityError: duplicate key value violates unique constraint "users_user_email_key"
DETAIL:  Key (email)=() already exists.

我没有看到所有新用户出现此错误。例如,在过去24小时内,我在以下时间收到此错误:

  • 13:52,13:59,19:32,19:33,21:34,22:13,22:15,22:29

但是,我已经让用户在以下时间成功通过Facebook注册:

  • 17:05,17:18,21:41,21:58,01:57,02:20,02:28,07:33,09:50,10:33,13:34,14:07

我认为注册成功的用户与获得错误的用户之间没有任何关系。我不相信这是尝试使用Facebook注册的现有用户的问题。一些第一次用户通知我他们已经收到错误。

似乎错误发生在create_user函数中。我的管道如下:

SOCIAL_AUTH_PIPELINE = (
    'social.pipeline.social_auth.social_details',
    'social.pipeline.social_auth.social_uid',
    'social.pipeline.social_auth.auth_allowed',
    'social.pipeline.social_auth.social_user',
    'social.pipeline.user.get_username',
    'social.pipeline.mail.mail_validation',
    'social.pipeline.social_auth.associate_by_email',
    'social.pipeline.user.create_user',
    'social.pipeline.social_auth.associate_user',
    'social.pipeline.social_auth.load_extra_data',
    'social.pipeline.user.user_details',
    'users.pipeline.user.store_gender',
    'users.pipeline.user.store_registration_type',
)

知道可能出现什么问题吗? Facebook注册在70%的时间里对我有用,但我在剩下的30%中得到了这个错误。似乎没有错误的模式,它真的让我循环。我应该迁移到其他身份验证服务吗?我正在使用Django 1.8和Python 2.7。

1 个答案:

答案 0 :(得分:2)

问题是,您的email字段是唯一的,并且您已经拥有''值的用户,这是一个空字符串。

正在发生的事情是,当用户尝试使用Facebook登录时,有时候email是空的(也许他没有获得从Facebook获取电子邮件的许可)。然后,当您尝试保存它时,它会发出此错误。

要验证这一点,请让用户收到空电子邮件并删除他:

user = User.objects.get(email='')
user.delete()

然后,再次尝试使用Facebook登录。不应该有任何错误。 现在再试一次。您将遇到同样的错误。

希望它有所帮助!