pip install django
pip install python-social-auth
./manage.py startproject mysite
INSTALLED_APPS = (
...
'social.apps.django_app.default',
...
)
...
# Social Authentication
# -----------------------
TEMPLATE_DIRS = (
os.path.join(BASE_DIR,'templates'),
)
from django.conf import global_settings
TEMPLATE_CONTEXT_PROCESSORS = global_settings.TEMPLATE_CONTEXT_PROCESSORS + (
'django.core.context_processors.request',
'social.apps.django_app.context_processors.backends',
'social.apps.django_app.context_processors.login_redirect',
)
AUTHENTICATION_BACKENDS = (
'social.backends.facebook.FacebookOAuth2',
'social.backends.google.GoogleOAuth2',
'social.backends.twitter.TwitterOAuth',
'django.contrib.auth.backends.ModelBackend',
)
SOCIAL_AUTH_FACEBOOK_KEY = '300242133500110'
SOCIAL_AUTH_FACEBOOK_SECRET = '7d0b59cca13d5d56edeed5963e612***'
SOCIAL_AUTH_FACEBOOK_SCOPE = ['email']
from django.views.generic import TemplateView
urlpatterns = patterns('',
...
url('', include('social.apps.django_app.urls', namespace='social')),
url(r'^$', TemplateView.as_view(template_name="home.html"), name='home'),
...
)
<html>
<body>
<h1>Third-party authentication demo</h1>
<p>
<ul>
{% if request.user.is_active %}
<li>
<a>Hello {{ request.user.get_full_name|default:request.user.username }}!</a>
</li>
<li>
<a href="/admin/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>
</body>
</html>
./manage.py makemigrations
./manage.py migrate
./manage.py runserver
visit http://localhost:8000