Social-auth-app-django(使用LinkedIn登录),用户未通过身份验证

时间:2018-06-20 16:16:46

标签: django python-social-auth django-socialauth

我能够从LinkedIn上获取所有用户详细信息到数据库中,并且每次都在创建一个新用户。如何“登录”创建的用户?

这些是在我的虚拟环境中安装的软件包:

certifi==2018.4.16
chardet==3.0.4
defusedxml==0.5.0
Django==1.10.8
idna==2.7
oauthlib==2.1.0
pkg-resources==0.0.0
PyJWT==1.6.4
python-social-auth==0.3.6
python3-openid==3.1.0
requests==2.19.1
requests-oauthlib==1.0.0
six==1.11.0
social-auth-app-django==2.1.0
social-auth-core==1.7.0
urllib3==1.23

这些是我在“ settings.py”中所做的更改:

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

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',
            ],
        },
    },
]

AUTHENTICATION_BACKENDS = (
    'social.backends.linkedin.LinkedinOAuth2',
    'django.contrib.auth.backends.ModelBackend',
)

SOCIAL_AUTH_LINKEDIN_OAUTH2_KEY = 'my_key'
SOCIAL_AUTH_LINKEDIN_OAUTH2_SECRET = 'my_secret'

SOCIAL_AUTH_LINKEDIN_OAUTH2_SCOPE =     ['r_basicprofile','r_emailaddress']
SOCIAL_AUTH_LINKEDIN_OAUTH2_FIELD_SELECTORS = ['email-address']
SOCIAL_AUTH_LINKEDIN_OAUTH2_EXTRA_DATA = [('id', 'id'),
                               ('firstName', 'first_name'),
                               ('lastName', 'last_name'),
                               ('emailAddress', 'email_address')]

SOCIAL_AUTH_LOGIN_REDIRECT_URL = '/home/'
SOCIAL_AUTH_LOGIN_URL = '/'

这是我的主要“ urls.py”:

from django.conf.urls import url, include
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^', include('accounts.urls')),
    url(r'', include('social_django.urls', namespace='social')),
]

这是“帐户”应用中的另一个“ urls.py”:

from django.conf.urls import url,include
from . import views

urlpatterns=[
    url(r'^$', views.login, name='login'),
    url(r'^home/$',views.home, name='home'),
] 

这是我的“ views.py”:

from django.shortcuts import render,redirect

def login(request):
    return render(request,'login.html')

def home(request):
    print(request.user)
    print(request.user.is_authenticated())
    return render(request,'home.html')

我的“ login.html”只有一行:

<a href="{% url 'social:begin' backend='linkedin-oauth2' %}">Login with Linkedin</a>

现在,在单击“使用LinkedIn登录”并授予网站访问用户配置文件的权限后,该用户将被重定向到“主视图”,并且两个打印语句将产生以下输出:

AnonymousUser
False

这是我的database的屏幕截图。

用户数据已成功保存在数据库中,但实际上并未登录。有人可以告诉我该怎么做吗?

0 个答案:

没有答案