Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django Social Auth中的AuthAlreadyAssociated异常_Python_Django_Django Socialauth - Fatal编程技术网

Python Django Social Auth中的AuthAlreadyAssociated异常

Python Django Social Auth中的AuthAlreadyAssociated异常,python,django,django-socialauth,Python,Django,Django Socialauth,在我使用Facebook(比方说fbuser)或Google(googleuser)创建一个用户之后。如果我通过普通django管理员(normaluser)创建另一个用户,并在第三个用户(normaluser)登录时尝试使用Facebook或Google再次登录,则会抛出错误异常AuthAlreadyAssociated 理想情况下,它应该抛出一个名为“您已作为用户登录”的错误 用户正常用户 或者它应该注销普通用户,并尝试与 已与FB或Google关联的帐户,如案例所示 可能是 如何实现上述两

在我使用Facebook(比方说fbuser)或Google(googleuser)创建一个用户之后。如果我通过普通django管理员(normaluser)创建另一个用户,并在第三个用户(normaluser)登录时尝试使用Facebook或Google再次登录,则会抛出错误异常AuthAlreadyAssociated

  • 理想情况下,它应该抛出一个名为“您已作为用户登录”的错误 用户正常用户

  • 或者它应该注销普通用户,并尝试与 已与FB或Google关联的帐户,如案例所示 可能是

  • 如何实现上述两个功能之一?欢迎大家指教


    此外,当我尝试自定义社交身份验证管道时,无法使用FB或Google登录,并且它强制登录URL/accounts/login/

    DSA此时不注销帐户(或刷新会话)
    AuthAlreadyAssociated
    突出显示当前用户未与尝试使用的当前社交帐户关联的场景。有两种解决方案可能适合您的项目:

  • 定义
    social\u auth.middleware.SocialAuthExceptionMiddleware
    的子类,并覆盖默认行为(
    process\u exception()
    )以您喜欢的方式重定向或设置警告

  • 添加一个管道方法(替换
    social\u auth.backend.pipeline.social.social\u auth\u user
    )以注销当前用户,而不是引发异常


  • 我处理这个问题的方法有点不同,我没有在管道中处理这个问题,而是首先确保从未将用户传递到管道中。这样,即使social_auth.user与登录用户不匹配,social_auth.user也将在当前登录用户的基础上登录

    我认为这就像覆盖
    完成
    操作一样简单

    url.py

    url(r'^complete/(?P<backend>[^/]+)/$', 'account.views.complete', name='complete'),
    

    对于想知道如何在python social auth版本3下覆盖social_用户管道的人的解决方案+

    在您的设置中。py:

    SOCIAL_AUTH_PIPELINE = (
        'social_core.pipeline.social_auth.social_details',
        'social_core.pipeline.social_auth.social_uid',
        'social_core.pipeline.social_auth.auth_allowed',
        # Path to your overrided method
        # You can set any other valid path.
        'myproject.apps.python-social-auth-overrided.pipeline.social_auth.social_user',
        'social_core.pipeline.user.get_username',
        'social_core.pipeline.user.create_user',
        'social_core.pipeline.social_auth.associate_user',
        'social_core.pipeline.social_auth.load_extra_data',
        'social_core.pipeline.user.user_details',
    )
    
    在覆盖的社交用户中

    from django.contrib.auth import logout
    
    def social_user(backend, uid, user=None, *args, **kwargs):
        provider = backend.name
        social = backend.strategy.storage.user.get_social_auth(provider, uid)
        if social:
            if user and social.user != user:
                logout(backend.strategy.request)
            elif not user:
                user = social.user
        return {'social': social,
                'user': user,
                'is_new': user is None,
                'new_association': False}
    

    如果需要,可以删除注释行。

    我也遇到了同样的问题。我通过在设置中插入下面的代码解决了这个问题

    AUTHENTICATION_BACKENDS = (
        '...',
        'social_core.backends.facebook.FacebookOAuth2',
        '...',
    )
    SOCIAL_AUTH_PIPELINE = (
        '...',
        'social_core.pipeline.user.user_details',
        '...',
    )
    
    我所做的是:

  • 定义从SocialAuthExceptionMiddleware继承的类

  • 执行方法
    处理异常

  • 将实现的类添加到
    设置.py
    上的
    中间件
    列表中

  • 在应位于应用程序目录中的
    中间件.py
    中,即与应用程序关联的
    视图.py
    文件的同一目录中,定义以下类:

    from django.shortcuts import redirect
    from django.urls import reverse
    
    from social_core.exceptions import AuthAlreadyAssociated
    
    class FacebookAuthAlreadyAssociatedMiddleware(SocialAuthExceptionMiddleware):
        """Redirect users to desired-url when AuthAlreadyAssociated exception occurs."""
        def process_exception(self, request, exception):
            if isinstance(exception, AuthAlreadyAssociated):
                if request.backend.name == "facebook":
                    message = "This facebook account is already in use."
                    if message in str(exception):
                        # Add logic if required
    
                        # User is redirected to any url you want
                        # in this case to "app_name:url_name"
                        return redirect(reverse("app_name:url_name"))
    
    设置.py
    中,将实现的类添加到
    中间件列表中:

    MIDDLEWARE = [
        # Some Django middlewares
        "django.middleware.security.SecurityMiddleware",
        "django.contrib.sessions.middleware.SessionMiddleware",
        "django.middleware.locale.LocaleMiddleware",
        "django.middleware.common.CommonMiddleware",
        "django.contrib.auth.middleware.AuthenticationMiddleware",
        "django.contrib.messages.middleware.MessageMiddleware",
        "django.middleware.clickjacking.XFrameOptionsMiddleware",
        "social_django.middleware.SocialAuthExceptionMiddleware",
    
        # the middleware you just implemented
        "app_name.middleware.FacebookAuthAlreadyAssociatedMiddleware",
    ]
    

    这解决了我的问题,当出现
    AuthAlreadyAssociated
    异常时,我能够处理控制流。

    我尝试执行选项2,但没有成功。它成功注销了用户,但没有作为新的social.user重新登录。替换:msg='此{0}帐户已在使用。'。格式(提供程序)将AuthAlreadyAssociated(strategy.backend,msg)提升为:logout(kwargs.get('request'))user=social。user@omab:在使用Django的python social auth上,如何在同一请求中无缝注销当前用户并使用social_用户管道替换为第二个用户设置会话?@omab:在social.actions.do_complete中,is_authenticated是基于现有用户(“用户A”)在开始时设置的。但是,如果我注销管道中的“用户A”并返回“用户B”,do_complete将不会登录“用户B”,因为它已被设置为True。do_complete应该在管道完成后再次重新评估会话用户,以确定是否登录“用户B”?随着时间的推移,我以前的解决方案似乎不再正常工作,可能是使用最新的python social auth。无论如何,您的解决方案确实可以正常工作,而且肯定比我的更简单。我已经把我的从这个问题上删除了。谢谢。由于原始的
    complete
    视图发生了轻微更改,因此无法正常工作。我不得不深入源代码并对其进行一些调整以匹配更改,但策略是可靠的,无条件地覆盖用户参数,不传递任何值。最新的完整函数(0.3.x版本)版本对我来说,您的方法是注销当前用户。但是它没有登录到新认证的用户。有什么帮助吗?不确定,这不是一个无效的答案。
    MIDDLEWARE = [
        # Some Django middlewares
        "django.middleware.security.SecurityMiddleware",
        "django.contrib.sessions.middleware.SessionMiddleware",
        "django.middleware.locale.LocaleMiddleware",
        "django.middleware.common.CommonMiddleware",
        "django.contrib.auth.middleware.AuthenticationMiddleware",
        "django.contrib.messages.middleware.MessageMiddleware",
        "django.middleware.clickjacking.XFrameOptionsMiddleware",
        "social_django.middleware.SocialAuthExceptionMiddleware",
    
        # the middleware you just implemented
        "app_name.middleware.FacebookAuthAlreadyAssociatedMiddleware",
    ]