AttributeError: 'dict' object has no attribute 'user' during overriding "social-app-auth-django" create_user

AttributeError: 'dict' object has no attribute 'user' during overriding "social-app-auth-django" create_user

我注意到 SAAD 无法将现有用户与社交身份验证登录用户相关联。这是创建具有相同电子邮件地址的多个用户。我试图通过在 settings.py、

中注释掉来覆盖它
# 'social_core.pipeline.user.create_user',

然后我创建了一个视图来创建一个用户配置文件。我添加了这个管道,

SOCIAL_AUTH_PIPELINE = (
...
'accounts.views.save_profile',
...
)

accounts.views

def save_profile(backend, user, response, *args, **kwargs):
    username = ""
    if backend.name == "google-oauth2":
        email = response["emails"][0]["value"]
        existing_user = User.objects.get(email=email)
        username = existing_user.username
        password = existing_user.password
        authenticated_user = authenticate(username=username, password=password)
        login(request,authenticated_user) # I need request to login the user 

这里的问题是我需要请求登录用户。登录需要请求和经过身份验证的用户作为参数。假设我添加请求作为参数,我得到一个 AttributeError: 'dict' object has no attribute 'user'

def save_profile(request, backend, user, response, *args, **kwargs):
    username = ""
    if backend.name == "google-oauth2":
        email = response["emails"][0]["value"]
        existing_user = User.objects.get(email=email)
        username = existing_user.username
        password = existing_user.password
        authenticated_user = authenticate(username=username, password=password)
        login(request,authenticated_user) 

如何在已知用户名和密码的情况下登录用户?

错误回溯:

Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/django/core/handlers/base.py", line 149, in get_response
response = self.process_exception_by_middleware(e, request)
  File "/Library/Python/2.7/site-packages/django/core/handlers/base.py", line 147, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Library/Python/2.7/site-packages/django/views/decorators/cache.py", line 57, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
  File "/Library/Python/2.7/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
return view_func(*args, **kwargs)
  File "/Library/Python/2.7/site-packages/social_django/utils.py", line 50, in wrapper
return func(request, backend, *args, **kwargs)
  File "/Library/Python/2.7/site-packages/social_django/views.py", line 28, in complete
redirect_name=REDIRECT_FIELD_NAME, *args, **kwargs)
  File "/Library/Python/2.7/site-packages/social_core/actions.py", line 41, in do_complete
user = backend.complete(user=user, *args, **kwargs)
  File "/Library/Python/2.7/site-packages/social_core/backends/base.py", line 39, in complete
return self.auth_complete(*args, **kwargs)
  File "/Library/Python/2.7/site-packages/social_core/utils.py", line 253, in wrapper
return func(*args, **kwargs)
  File "/Library/Python/2.7/site-packages/social_core/backends/oauth.py", line 398, in auth_complete
*args, **kwargs)
  File "/Library/Python/2.7/site-packages/social_core/utils.py", line 253, in wrapper
return func(*args, **kwargs)
  File "/Library/Python/2.7/site-packages/social_core/backends/oauth.py", line 409, in do_auth
return self.strategy.authenticate(*args, **kwargs)
  File "/Library/Python/2.7/site-packages/social_django/strategy.py", line 115, in authenticate
return authenticate(*args, **kwargs)
  File "/Library/Python/2.7/site-packages/django/contrib/auth/__init__.py", line 74, in authenticate
user = backend.authenticate(**credentials)
  File "/Library/Python/2.7/site-packages/social_core/backends/base.py", line 79, in authenticate
return self.pipeline(pipeline, *args, **kwargs)
  File "/Library/Python/2.7/site-packages/social_core/backends/base.py", line 82, in pipeline
out = self.run_pipeline(pipeline, pipeline_index, *args, **kwargs)
  File "/Library/Python/2.7/site-packages/social_core/backends/base.py", line 107, in run_pipeline
result = func(*args, **out) or {}
  File "/Users/swaggerjeevan07/Desktop/django/mysite/accounts/views.py", line 76, in save_profile
login(request,authenticated_user)
  File "/Library/Python/2.7/site-packages/django/contrib/auth/__init__.py", line 97, in login
user = request.user     # This is the issue 
AttributeError: 'dict' object has no attribute 'user'

我所做的只是将 social_core.pipeline.social_auth.associate_by_email 添加到 create_user 上方的管道中。现在,它检查是否有一个帐户已经与获得的社交身份验证电子邮件相关联。如果没有,它会创建一个新用户。

SOCIAL_AUTH_PIPELINE = (
    ...
    'social_core.pipeline.social_auth.associate_by_email',
    'social_core.pipeline.user.create_user',
    'accounts.views.save_profile',
    'social_core.pipeline.social_auth.associate_user',
    ...
)

def save_profile 看起来像:

def save_profile(backend, user, response, *args, **kwargs):
    if backend.name == "google-oauth2":
        # A dot in username will have url issues 
        username = user.username.replace(".","_") 
        user.username = username
        user.first_name = user.first_name.capitalize()
        user.last_name = user.last_name.capitalize()