python-social-auth

Official Ref Link

Login with twitter tutorial

Login with facebook, google, twitter


pip install python-social-auth

Settings.py

Add 'social.apps.django_app.default' in installed apps

INSTALLED_APPS = [
    ...
    'social.apps.django_app.default',
]
AUTHENTICATION_BACKENDS = (
    'social.backends.open_id.OpenIdAuth',
    'social.backends.google.GoogleOpenId',
    'social.backends.google.GoogleOAuth2',
    'social.backends.google.GoogleOAuth',
    'social.backends.twitter.TwitterOAuth',
    'social.backends.facebook.FacebookOAuth2',
    'social.backends.yahoo.YahooOpenId',
    'django.contrib.auth.backends.ModelBackend',
)
SOCIAL_AUTH_PIPELINE = (
    'social.pipeline.social_auth.social_details',
    'social.pipeline.social_auth.social_uid',
    'social.pipeline.social_auth.auth_allowed',
    'social.pipeline.social_auth.social_user',
    'social.pipeline.user.get_username',
    'social.pipeline.user.create_user',
    'social.pipeline.social_auth.associate_user',
    'social.pipeline.social_auth.load_extra_data',
    'social.pipeline.user.user_details',
    'social_auth_demo.views.save_profile',  # <--- set the path to the function
)

ROOT_URLCONF = 'social_auth_demo.urls'
SOCIAL_AUTH_URL_NAMESPACE = 'social'

from .config import *

config.py

SOCIAL_AUTH_TWITTER_KEY = ''
SOCIAL_AUTH_TWITTER_SECRET = ''

SOCIAL_AUTH_FACEBOOK_KEY = ''
SOCIAL_AUTH_FACEBOOK_SECRET = ''

SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = ''
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = ''

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

LOGIN_REDIRECT_URL = '/'

urls.py

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

admin.autodiscover()

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url('', include('social.apps.django_app.urls', namespace='social')),
    url('', include('django.contrib.auth.urls', namespace='auth')),
    url(r'^$', home, name='home'),
]

admin.py

SOCIAL_AUTH_ADMIN_USER_SEARCH_FIELDS = ['username', 'first_name', 'email']

views.py

from django.shortcuts import render_to_response, redirect, render
from django.contrib.auth import logout as auth_logout
from django.contrib.auth.decorators import login_required
from django.template.context import RequestContext


def home(request):
   context = RequestContext(request, {'user': request.user})
   return render_to_response('home.html', context_instance=context)


def save_profile(strategy, details, backend, response, uid, user, *args, **kwargs):    
    social = kwargs.get('social') or strategy.storage.user.get_social_auth(
        backend.name,
        uid
    )

    if backend.name == 'google-oauth2':
        social.set_extra_data({'image': response.get('image', '')})
        social.set_extra_data({'occupation': response.get('occupation', '')})
        social.set_extra_data({'id_token': response.get('id_token', '')})
        social.set_extra_data({'token_type': response.get('token_type', '')})
        social.set_extra_data({'url': response.get('url', '')})
        social.set_extra_data({'verified': response.get('verified', '')})
        social.set_extra_data({'id': response.get('id', '')})
        social.set_extra_data({'organizations': response.get('organizations', '')})
        social.set_extra_data({'kind': response.get('kind', '')})
        social.set_extra_data({'displayName': response.get('displayName', '')})
        social.set_extra_data({'name': response.get('name', '')})
        social.set_extra_data({'language': response.get('language', '')})
        social.set_extra_data({'access_token': response.get('access_token', '')})
        social.set_extra_data({'gender': response.get('gender', '')})
        social.set_extra_data({'expires_in': response.get('expires_in', '')})
        social.set_extra_data({'emails': response.get('emails', '')})
        social.set_extra_data({'skills': response.get('skills', '')})
        social.set_extra_data({'isPlusUser': response.get('isPlusUser', '')})
        social.set_extra_data({'etag': response.get('etag', '')})
        social.set_extra_data({'objectType': response.get('objectType', '')})

home.html

{% extends 'base.html' %}

{% block main %}
 <div>
 <h1>Third-party authentication demo</h1>

 <p>
   <ul>
   {% if user and not user.is_anonymous %}
     <li>
       <a>Hello {{ user.get_full_name|default:user.username }}!</a>
     </li>
     <li>
       <a href="{% url 'auth:logout' %}?next={{ request.path }}">Logout</a>
     </li>
   {% else %}
     <li>
       <a href="{% url 'social:begin' 'facebook' %}?next={{ request.path }}">Login with Facebook</a>
     </li>
     <li>
       <a href="{% url 'social:begin' 'google-oauth2' %}?next={{ request.path }}">Login with Google</a>
     </li>
     <li>
       <a href="{% url 'social:begin' 'twitter' %}?next={{ request.path }}">Login with Twitter</a>
     </li>
   {% endif %}
   </ul>
 </p>
 </div>
{% endblock %}

Home Page social_auth

results matching ""

    No results matching ""