DEV Community

Moin Ul Haq
Moin Ul Haq

Posted on

LinkedIn OAuth in Django Using Allauth and OpenID Connect: A Practical Guide

By Moin ul Haq – Software Engineer | Web Developer | Django Enthusiast

Integrating LinkedIn OAuth in Django can be tricky, especially when you want smooth authentication and proper user data handling. In this guide, I’ll walk you through using Django Allauth with OpenID Connect for LinkedIn login, tackling real-world issues I faced while building client-facing web apps.

Django allauth linkedin openid connect
Why LinkedIn OAuth?
LinkedIn login is perfect when you want:

  • Users to sign up quickly using their LinkedIn profiles.
  • Secure authentication without managing passwords.
  • Access to professional user data like email, name, and profile URL.

Most tutorials cover Google or Facebook, but LinkedIn’s OpenID Connect implementation often confuses developers. Let’s solve it step by step.

Step 1: Install Dependencies

Start with Django and Allauth:

pip install django
pip install django-allauth[openidconnect]
Enter fullscreen mode Exit fullscreen mode

Make sure your Django project is set up with INSTALLED_APPS updated:

INSTALLED_APPS = [
    ...
    'django.contrib.sites',
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    'allauth.socialaccount.providers.openid_connect',
]
SITE_ID = 1
Enter fullscreen mode Exit fullscreen mode

Step 2: Configure LinkedIn as OpenID Connect Provider

  1. Go to LinkedIn Developer Portaland create a new app.
  2. Add OAuth 2.0 redirect URLs, e.g., http://localhost:8000/accounts/openid/login/callback/
  3. Note your Client ID and Client Secret.

Step 3: Add Provider in Django Admin

Django Allauth allows dynamic configuration:

  1. Login to Django Admin → Social Applications → Add LinkedIn.
  2. Select Site → example.com (or localhost for testing).
  3. Enter Client ID and Secret Key.
  4. Use OpenID Connect as Provider type.

Step 4: Update URLs

Add Allauth URLs to your urls.py:

from django.urls import path, include

urlpatterns = [
    path('accounts/', include('allauth.urls')),
    ...
]
Enter fullscreen mode Exit fullscreen mode

Step 5: Frontend Integration

Add a simple login button in your template:

<a href="{% provider_login_url 'openid_connect' %}?process=login">
    Login with LinkedIn
</a>
Enter fullscreen mode Exit fullscreen mode

⚠️ Tip: Always use {% provider_login_url %} instead of hardcoding URLs to avoid callback issues.

Step 6: Handling Common Problems

1. “Invalid redirect_uri”

  • Make sure the redirect URI in LinkedIn app matches exactly with the one Django Allauth uses.

2. User Email Not Returned

  • LinkedIn requires r_emailaddress permission. Ensure your Allauth provider scope includes:
SOCIALACCOUNT_PROVIDERS = {
    'openid_connect': {
        'SCOPE': ['openid', 'email', 'profile']
    }
}
Enter fullscreen mode Exit fullscreen mode

3. Tokens Not Saved

  • Allauth stores tokens in SocialToken. Always check the SocialApp and SocialAccount entries in Django Admin.

Step 7: Verify Everything Works

  1. Run server:
    python manage.py runserver

  2. Click Login with LinkedIn → authorize → redirected to dashboard.

  3. Check request.user → should have LinkedIn email, name, and token.

Personal Branding Angle

While implementing this, I realized professional networking login flows not only save development time but also improve user trust. As a developer, mastering OAuth integrations with Django is a strong portfolio highlight, showing clients that you can handle enterprise-grade authentication.

💡 Pro Tip: Post your projects integrating OAuth on GitHub with proper README and a live demo. Recruiters and clients love this.

Conclusion

Using Django Allauth + OpenID Connect makes LinkedIn OAuth integration straightforward if you handle:

  • Provider scopes correctly.
  • Redirect URIs precisely.
  • Tokens and user data securely.

This setup not only improves user experience but also demonstrates your capability to solve complex authentication challenges professionally.

🔗 About Me

I’m Moin ul Haq, a software engineer building scalable Django applications. I write about real-world solutions, web development challenges, and software best practices. Check out my projects on GitHub.

Top comments (0)