【问题标题】:How to handle AuthAlreadyAssociated at Python-auth/Django-Social?如何在 Python-auth/Django-Social 处理 AuthAlreadyAssociated?
【发布时间】:2018-04-18 23:31:19
【问题描述】:

我使用此python-social-auth/social-app-django 将我的网络与社交媒体连接起来 想问一下同一个账号注册报错怎么处理?

例如,我使用 facebook 注册,然后再次使用 twitter 注册。两者都成功注册到两个单独的帐户,当我使用我的 Facebook 登录,然后在我的帐户设置页面上,我想连接我已经注册的 Twitter,然后它会显示错误消息

“AuthAlreadyAssociated at /oauth/complete/twitter/”

AuthAlreadyAssociated

当我在 twitter 重定向页面上授权后,当它被重定向回我的网站时,会出现此消息。

总之,已经注册在其他账户的账户怎么处理?

这是我的views.py:

@login_required
def settings(request):
    user = request.user
    try:
        github_login = user.social_auth.get(provider='github')
    except UserSocialAuth.DoesNotExist:
        github_login = None

    try:
        twitter_login = user.social_auth.get(provider='twitter')
    except UserSocialAuth.DoesNotExist:
        twitter_login = None

    try:
        facebook_login = user.social_auth.get(provider='facebook')
    except UserSocialAuth.DoesNotExist:
        facebook_login = None

    can_disconnect = (user.social_auth.count() > 1 or 
    user.has_usable_password())

    return render(request, 'profile/settings.html', {
        'github_login': github_login,
        'twitter_login': twitter_login,
        'facebook_login': facebook_login,
        'can_disconnect': can_disconnect
    })

这是我的 settings.html 模板:

{% extends "base.html" %}
{% load crispy_forms_tags %}
{% block title %}Navhi Microblog - Profile{% endblock %}
{% block content %}
<div class="col-lg-12 mx-auto">
    <br />
    <div class="card">
        <div class="card-body">
            {% include 'profile/base_profile.html' %}
                
                <!--Card Body goes here-->
                <div class="card-body">
                    <div class="card">
                        <div class="card-body">
                            {% if github_login %}
                                {% if can_disconnect %}
                                    <form method="post" action="{% url 'social:disconnect' 'github' %}">
                                        {% csrf_token %}
                                        <button class="btn btn-danger btn-block" type="submit">Disconnect from GitHub</button>
                                    </form>
                                {% else %}
                                    <p class="text-center" style="color: red">You must <a href="{% url 'password' %}">define a password</a> for your account before disconnecting from Github.</<p class="text-center">
                                {% endif %}
                            {% else %}
                                <a class="btn btn-sm btn-social btn-github btn-block" href="{% url 'social:begin' 'github' %}?next={{ request.path }}">
                                    <span class="fa fa-github"></span> Connect to Github
                                </a>
                            {% endif %}
                        </div>
                    </div>
                    <br />
                    <div class="card">
                        <div class="card-body">
                            {% if twitter_login %}
                                {% if can_disconnect %}
                                    <form method="post" action="{% url 'social:disconnect' 'twitter' %}">
                                        {% csrf_token %}
                                        <button class="btn btn-danger btn-block" type="submit">Disconnect from Twitter</button>
                                    </form>
                                {% else %}
                                    <p class="text-center" style="color: red">You must <a href="{% url 'password' %}">define a password</a> for your account before disconnecting from Twitter.</p>
                                {% endif %}
                            {% else %}
                                <a class="btn btn-sm btn-social btn-twitter btn-block" href="{% url 'social:begin' 'twitter' %}?next={{ request.path }}">
                                    <span class="fa fa-twitter"></span> Connect to Twitter
                                </a>
                            {% endif %}
                        </div>
                    </div>
                    <br />
                    <div class="card">
                        <div class="card-body">
                            {% if facebook_login %}
                                {% if can_disconnect %}
                                    <form method="post" action="{% url 'social:disconnect' 'facebook' %}">
                                        {% csrf_token %}
                                        <button class="btn btn-danger btn-block" type="submit">Disconnect from Facebook</button>
                                    </form>
                                {% else %}
                                    <p class="text-center" style="color: red">You must <a href="{% url 'password' %}">define a password</a> for your account before disconnecting from Facebook.</p>
                                {% endif %}
                            {% else %}
                                <a class="btn btn-sm btn-social btn-facebook btn-block" href="{% url 'social:begin' 'facebook' %}?next={{ request.path }}">
                                    <span class="fa fa-facebook"></span> Connect to Facebook
                                </a>
                            {% endif %}
                        </div>
                    </div>
                </div>         
            </div>
        </div>
    </div>
</div>
{% endblock %}

谢谢你,我希望有人可以为此提供解决方案,对不起我的英语不好。

【问题讨论】:

标签: python django facebook twitter oauth


【解决方案1】:

您的问题已在此处得到解答: AuthAlreadyAssociated Exception in Django Social Auth

基本上答案是覆盖social_auth.middleware.SocialAuthExceptionMiddleware类中的默认方法process_exception(),并将这个中间件添加到你的settings.py中。

更多关于如何覆盖这里:How do I handle exceptions on Python Social Auth

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-18
    • 2017-08-31
    • 2015-10-25
    • 2014-03-26
    相关资源
    最近更新 更多