【问题标题】:AttributeError: 'dict' object has no attribute 'user' during overriding "social-app-auth-django" create_userAttributeError:“dict”对象在覆盖“social-app-auth-django”create_user 期间没有属性“user”
【发布时间】:2017-07-14 19:30:26
【问题描述】:

我注意到SAAD 无法将现有用户与已登录的 social-auth 用户相关联。这是使用相同的电子邮件地址创建多个用户。我试图通过在 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'

【问题讨论】:

    标签: django django-models django-authentication python-social-auth django-login


    【解决方案1】:

    我所做的只是将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()
    

    【讨论】:

      猜你喜欢
      • 2014-11-11
      • 1970-01-01
      • 2021-02-14
      • 2020-03-29
      • 2022-12-15
      • 2016-12-17
      • 2018-09-02
      • 2021-01-30
      • 2020-09-11
      相关资源
      最近更新 更多