简体   繁体   中英

Authenticating with Linkedin using python-social-auth

I'm looking to allow users to sign into my web app either by creating a profile (done through django.contrib.auth ), or using LinkedIn (done through python-social-auth ).

Relevant file hierarchy is as follows:

.
├── ./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

Thus far, I've added the following entries into my 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,
    },
},]

In proj/urls.py , I've added the following entry:

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

On my login.html page, I've added a link:

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

My login view in app/views.py looks like this:

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})

Currently, my "sign in with LinkedIn" link fails to get picked up by the python-social-auth backend. As pointed out in this tutorial , I've confirmed that python-social-auth 's backend is working: I can see empty tables on the site's admin page.

I've read the following tutorials or SO entries to try and answer my question:

  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

Any idea what I'm missing?

from here

Change:

SOCIAL_AUTH_LINKEDIN_SCOPE = ['r_basicprofile']

to:

SOCIAL_AUTH_LINKEDIN_OAUTH2_SCOPE = ['r_basicprofile']

you might want to use the following as well:

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

(though I'm having problems getting the email address)

Found the solution for missing emails

Add the following to you settings.py file:

FIELD_SELECTORS = ['email-address',]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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