使用python-social-auth通过Linkedin进行身份验证

时间:2016-06-15 17:28:31

标签: python django python-social-auth

我希望允许用户通过创建个人资料(通过django.contrib.auth完成)或使用LinkedIn(通过python-social-auth完成)来登录我的网络应用。

相关文件层次结构如下:

.
├── ./app
│   ├── ./app/admin.py
│   ├── ./app/forms.py
│   ├── ./app/__init__.py
│   ├── ./app/models.py
│   ├── ./app/serializers.py
│   ├── ./app/templates
│   │   ├── ./app/templates/app
│   │   │   ├── ./app/templates/app/app.js
│   │   │   ├── ./app/templates/app/create_profile.html
│   │   │   ├── ./app/templates/app/index.html
│   │   │   ├── ./app/templates/app/login.html
│   │   │   ├── ./app/templates/app/profile.html
│   │   │   ├── ./app/templates/app/signup.html
│   │   │   └── ./app/templates/app/userhome.html
│   ├── ./app/tests.py
│   ├── ./app/urls.py
│   ├── ./app/views.py
├── ./proj
│   ├── ./proj/settings.py
│   ├── ./proj/urls.py
│   ├── ./proj/wsgi.py
└── ./manage.py

到目前为止,我已将以下条目添加到我的settings.py

INSTALLED_APPS = (
    'django.contrib.auth',
    # Entries for other purposes...
    # ...
    # ...
    'social.apps.django_app.default'
)


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

SOCIAL_AUTH_LINKEDIN_KEY = '*secret*'
SOCIAL_AUTH_LINKEDIN_SECRET = '*secret*'
SOCIAL_AUTH_LINKEDIN_SCOPE = [ 'r_basicprofile' ]
LOGIN_REDIRECT_URL = '/userhome'

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [
    ],
    'APP_DIRS': True,
    'OPTIONS': {
        'context_processors': [
            # Entries for other purposes...
            # ...
            # ...
            'social.apps.django_app.context_processors.backends',
           'social.apps.django_app.context_processors.login_redirect',
        ],
        'debug': DEBUG,
    },
},]

proj/urls.py中,我添加了以下条目:

url('', include('social.apps.django_app.urls', namespace='social')),

在我的login.html页面上,我添加了一个链接:

<br>Or, <a href="/login/linkedin">sign in with LinkedIn.</a>

app/views.py中的我的登录视图如下所示:

def login (request):
    form = LoginForm()
    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():
            username = request.POST.get('username')
            password = request.POST.get('password')
            user = authenticate(username=username, password=password)
            if user is not None:
                auth_login(request, user)
                return redirect('/userhome/')
            else:
                messages.error(request, 'Invalid login or password')
        else:
            messages.error(request, 'Missing username or password')
    return render(request, 'app/login.html', {'form': form})

目前,我正在使用LinkedIn&#34;链接无法被python-social-auth后端获取。正如this tutorial中指出的那样,我已确认python-social-auth的后端正在运行:我可以在网站的管理页面上看到空表。

我已阅读以下教程或SO条目以尝试回答我的问题:

  1. Using python-social-auth with linkedin
  2. Python Social Auth Django template example
  3. https://python-social-auth.readthedocs.io/en/latest/configuration/django.html
  4. https://github.com/omab/python-social-auth/blob/master/examples/django_example/example/settings.py
  5. 知道我错过了什么吗?

1 个答案:

答案 0 :(得分:4)

来自here

变化:

SOCIAL_AUTH_LINKEDIN_SCOPE = ['r_basicprofile']

为:

SOCIAL_AUTH_LINKEDIN_OAUTH2_SCOPE = ['r_basicprofile']

您可能也想使用以下内容:

SOCIAL_AUTH_LINKEDIN_OAUTH2_FIELD_SELECTORS = ['email-address', 'headline', 'industry']
SOCIAL_AUTH_LINKEDIN_OAUTH2_EXTRA_DATA      = [
    ('id', 'id'),
    ('first-name', 'first_name'),
    ('last-name', 'last_name'),
    ('email-address', 'email_address'),
    ('headline', 'headline'),
    ('industry', 'industry')
]

(虽然我在收到电子邮件地址时遇到问题)

找到丢失电子邮件的解决方案

将以下内容添加到settings.py文件中:

FIELD_SELECTORS = ['email-address',]