gpt4 book ai didi

python - "Backend not found"Django 社交认证

转载 作者:行者123 更新时间:2023-12-01 02:08:08 25 4
gpt4 key购买 nike

当我尝试使用 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

最佳答案

您需要在AUTHENTICATON_BACKENDS中添加'django.contrib.auth.backends.ModelBackend',

AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
'social_core.backends.github.GithubOAuth2',
'social_core.backends.twitter.TwitterOAuth',
'social_core.backends.facebook.FacebookOAuth2',
'social_core.backends.google.GoogleOAuth2',
)

关于python - "Backend not found"Django 社交认证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48863307/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com