繁体   English   中英

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

[英]Authenticating with Linkedin using python-social-auth

我希望允许用户通过创建配置文件(通过django.contrib.auth完成)或使用LinkedIn(通过python-social-auth完成)登录我的Web应用程序。

相关文件层次结构如下:

.
├── ./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登录”链接无法被python-social-auth后端获取。 正如本教程中指出的那样,我已经确认python-social-auth的后端可以正常工作:我可以在网站的管理页面上看到空表。

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

  1. 在Linkedin上使用python-social-auth
  2. Python Social Auth Django模板示例
  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

知道我缺少什么吗?

这里

更改:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM