“后端未找到”django社会认证

时间:2018-02-19 09:44:56

标签: python django

当我尝试使用Google或Facebook进行社交授权时,我会在两个社交网站上都无法获得此错误。

Backend not found

有人可以在 social_django 设置中找到错误吗?

设置

INSTALLED_APPS = [
    ...
    'social_django',
    ...
]

MIDDLEWARE_CLASSES = [
    ...
    'social_django.middleware.SocialAuthExceptionMiddleware'
]

TEMPLATES = [
    {
     ...
        'OPTIONS': {
            'context_processors': [
                ...
                'social_django.context_processors.backends',
                'social_django.context_processors.login_redirect',
            ],
        },
    },
]
AUTHENTICATON_BACKENDS = (
    'social_core.backends.facebook.FacebookOAuth2',
    'social_core.backends.google.GoogleOAuth2',
    'users.backends.AuthBackend',
)
SOCIAL_AUTH_URL_NAMESPACE = 'social'

SOCIAL_AUTH_FACEBOOK_KEY = '...'
SOCIAL_AUTH_FACEBOOK_SECRET = '...'

SOCIAL_AUTH_GOOGLE_OAUTH2_KEY ='...'
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = '...'

urls.py

urlpatterns = [
    url('', include('social_django.urls', namespace='social')),
    ...
]

模板

<a href="{% url 'social:begin' backend='google-oauth2' %}">FB</a>
<a href="{% url 'social:begin' backend='facebook' %}">GOOGLE</a

users.backend.AuthBackend

from django.db.models import Q
from django.contrib.auth.backends import ModelBackend

from users.models import User


class AuthBackend(ModelBackend):
    supports_object_permissions = True
    supports_anonymous_user = False
    supports_inactive_user = False

    def authenticate(self, username, password):
        try:
            user = User.objects.filter(is_active=True).get(Q(email=username) | Q(phone_number=username))
        except User.DoesNotExist:
            return None
        return user if user.check_password(password) else None

2 个答案:

答案 0 :(得分:2)

您需要在 AUTHENTICATON_BACKENDS 中添加 src_ip Timestamp dest_ip_list 1 103.8.44.5 2018-02-13 12:42:00 192.168.1.162 2 167.89.123.54 2018-02-13 12:40:00 192.168.1.173 3 192.168.1.124 2018-02-13 12:40:00 216.58.221.35,24.156.242.137 4 192.168.1.124 2018-02-13 12:42:00 24.156.242.137 5 192.168.1.162 2018-02-13 12:42:00 103.8.44.5 6 192.168.1.170 2018-02-13 12:40:00 216.58.196.202 7 192.168.1.173 2018-02-13 12:40:00 167.89.123.54 8 216.58.196.202 2018-02-13 12:40:00 192.168.1.170 9 216.58.221.35 2018-02-13 12:40:00 192.168.1.124 10 24.156.242.137 2018-02-13 12:42:00 192.168.1.124

'django.contrib.auth.backends.ModelBackend',

答案 1 :(得分:0)

在设置文件的AUTHENTICATON_BACKENDS中添加 django.contrib.auth.backends.ModelBackend