2017-08-29 75 views
0

我的應用使用python-social-auth(0.2.11)同時使用電子郵件註冊和Facebook或Google+註冊。當一些新用戶註冊使用Facebook,他們被帶到錯誤頁面。在那之後,我看到了以下錯誤:密鑰(email)=()已經存在錯誤:python-social-auth

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,9: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', 
) 

任何想法什麼可能是錯的?大約70%的時間,Facebook註冊工作對我來說很有效,但在剩下的30%中我得到了這個錯誤。似乎沒有錯誤的模式,它真的讓我陷入循環。我應該遷移到不同的身份驗證服務嗎?我正在使用Django 1.8和Python 2.7。

回答

1

問題是,您的email字段是唯一的,並且您已經有一個用戶值爲'',這是一個空字符串。

發生什麼事情是,當用戶試圖用Facebook登錄時,有時email是空的(也許他沒有給予從Facebook獲取電子郵件的許可)。然後,當你試圖保存它時,它會發出這個錯誤。

爲了驗證這一點,得到與空的電子郵件用戶,並刪除了他:

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

然後,再次嘗試登錄電子賓館。不應該有任何錯誤。 現在再試一次。你會有這個相同的錯誤。

希望它有幫助!

+0

謝謝!所以事實證明,我有一個用戶沒有電子郵件。我已經修復了該用戶,因爲我知道這個用戶並且不希望他們丟失他們的數據。那些錯誤的人也不會分享他們的電子郵件嗎?如果是這樣,他們會創建一個空白的電子郵件? – wraasch

+0

@wraasch正確。他們沒有分享他們的電子郵件或其他原因,您無法從Facebook獲得他們的電子郵件。是的,第一個人將有一個空白的電子郵件,但與其他人,你會有這個錯誤。 –

+0

是否有通用的管道需要電子郵件?或者可能創建一個虛擬電子郵件以避免錯誤?我標記了正確的順便說一句。再次感謝! – wraasch

相關問題