Facebook没有找到后端

时间:2018-01-11 19:00:53

标签: python django facebook python-social-auth

我似乎无法让人工作,在阅读类似的问题之后我的问题不是由于没有指定密钥和秘密也不是因为我忘了在facebook之后添加'-oauth2'。 请帮忙??

我在浏览器上看到的只有Backend not Found。也许这只是因为我的django或python版本太新了? 我使用python 3.6和django 2.0 ......

这是我的项目的settings.py

SECRET_KEY = '<my_secret_key>'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'main_app',
    'django_filters',
    'cuentas',
    'social_django',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'social_django.middleware.SocialAuthExceptionMiddleware',
]

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                'social_django.context_processors.backends',
                'social_django.context_processors.login_redirect',
            ],
        },
    },
]

# python social auth settings
SOCIAL_AUTH_FACEBOOK_KEY = '<facebook_app_id>'

SOCIAL_AUTH_FACEBOOK_SECRET = '<facebook_app_secret>'

SOCIAL_AUTH_FACEBOOK_SCOPE = ['email']

SOCIAL_AUTH_FACEBOOK_API_VERSION = '2.11'

AUTHENTICATION_BACKENDS = [
    'social_core.backends.facebook.FacebookAppOAuth2',
    'social_core.backends.facebook.FacebookOAuth2',
    'django.contrib.auth.backends.ModelBackend',
]

SOCIAL_AUTH_URL_NAMESPACE = 'social'

SOCIAL_AUTH_PIPELINE = (
    'social_core.pipeline.social_auth.social_details',
    'social_core.pipeline.social_auth.social_uid',
    'social_core.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',
    'social_core.pipeline.social_auth.associate_by_email',
)

更新......这是我的主应用程序的urls.py

from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path('', include('social_django.urls', namespace='social')),
    path('admin/', admin.site.urls),
    path('', include('main_app.urls') ),
    path('cuentas/', include('cuentas.urls')),
]

if settings.DEBUG is True:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

这是登录facebook html链接

<a href="{% url 'social:begin' 'facebook-oauth2' %}">Facebook</a>

我刚刚在settings.py中添加了配置以输出更多日志信息,所以这是我尝试使用facebook登录时的输出

Performing system checks..
System check identified no issues (0 silenced).
January 12, 2018 - 11:23:23
Django version 2.0, using settings 'jaguamigos.settings'
Starting development server at http://127.0.0.1:80/
Quit the server with CTRL-BREAK.
Not Found: /favicon.ico
Not Found: /favicon.ico
Not Found: /login/facebook-oauth2/
Not Found: /login/facebook-oauth2/

2 个答案:

答案 0 :(得分:3)

omab绝对正确,插入facebook后端的name属性就可以了。

# Use 'facebook' instead of 'facebook-oauth2'
{% url 'social:begin' 'facebook' %}

答案 1 :(得分:2)

你可以尝试这个insted

<a href="{% url 'social:begin' 'facebook' %}">Login with Facebook</a>

AUTHENTICATION_BACKENDS = [
'social_core.backends.facebook.FacebookOAuth2',
'django.contrib.auth.backends.ModelBackend',
]

删除FacebookAppOAuth2。