12

I'm trying to use the Django Social Auth package to connect with Twitter, but I'm having difficulty understanding how exactly to do this as I can't find any examples. I am assuming that Django Social Auth is the best package to use for this purpose.

I've looked at a few examples that use Facebook, and from this have added the following to my settings.py file:

AUTHENTICATION_BACKENDS = (
    'social_auth.backends.twitter.TwitterBackend',
    'django.contrib.auth.backends.ModelBackend',
)

# overwriting default templates
TEMPLATE_CONTEXT_PROCESSORS = (    
    'django.core.context_processors.static',
    'django.core.context_processors.debug',
    'django.core.context_processors.i18n',
    'django.core.context_processors.media',
    'django.contrib.messages.context_processors.messages',
    'social_auth.context_processors.social_auth_by_type_backends',
    'django.contrib.auth.context_processors.auth',
)

SOCIAL_AUTH_ENABLED_BACKENDS = ('twitter')
SOCIAL_AUTH_DEFAULT_USERNAME = 'new_social_auth_user'

# Social media login info:
TWITTER_CONSUMER_KEY         = 'xxx'
TWITTER_CONSUMER_SECRET      = 'xxxxxx'

# 'magic' settings
SOCIAL_AUTH_COMPLETE_URL_NAME = 'socialauth_complete'
SOCIAL_AUTH_ASSOCIATE_URL_NAME = 'associate_complete'

SOCIAL_AUTH_PIPELINE = (
    'social_auth.backends.pipeline.social.social_auth_user',
    'social_auth.backends.pipeline.associate.associate_by_email',
    'social_auth.backends.pipeline.misc.save_status_to_session',
    'social.pipeline.redirect_to_form',
    'social.pipeline.username',
    'social_auth.backends.pipeline.user.create_user',
    'social_auth.backends.pipeline.social.associate_user',
    'social_auth.backends.pipeline.social.load_extra_data',
    'social_auth.backends.pipeline.user.update_user_details',
    'social_auth.backends.pipeline.misc.save_status_to_session',
    'social.pipeline.redirect_to_form2',
    'social.pipeline.first_name',
)

SOCIAL_AUTH_FORCE_POST_DISCONNECT = True
SOCIAL_AUTH_SESSION_EXPIRATION = False 

In urls.py I've added the following lines:

url('', include('social_auth.urls')),
url(r'^twitter/', twitter_app, name='twitter_app')

And in a file called twitter.py I've added the following to create a view:

from django.contrib.auth import BACKEND_SESSION_KEY
from django.contrib.auth.models import AnonymousUser
from django.http import HttpResponse
from django.http import HttpResponseRedirect #dq
from django.shortcuts import render_to_response
from django.template.context import RequestContext

from django.views.decorators.csrf import csrf_exempt
from django.core.cache import cache

from social_auth.models import UserSocialAuth
from social_auth.views import complete as social_complete
from social_auth.utils import setting
from social_auth.backends.twitter import TwitterBackend


# twitter login    
def twitter_app(request):
    """twitter login"""
    if request.user.is_authenticated():
        return HttpResponseRedirect('done')
    else:
        return render_to_response('twitter.html', {'twitter_app_id':setting('TWITTER_CONSUMER_KEY'),
                                                'warning': request.method == 'GET'}, RequestContext(request))

I've then created a template file called twitter.html with the following structure:

{% extends "base.html" %}

{% block script %}

Login with <a href="{% url socialauth_begin 'twitter' %}">Twitter</a>

{% endblock %}

This results in the following error message:

The webpage at http://example.com/twitter/done has resulted in too many redirects.

I'm a little bit lost as to what I should be doing overall. I have created an app on twitter with my site url to generate the api/secret key. Any advice on what direction I should go, or links to working examples would be greatly appreciated.

Cœur
  • 32,421
  • 21
  • 173
  • 232
djq
  • 13,348
  • 42
  • 111
  • 148
  • 1
    The URL to start the twitter authentication should be `{% url socialauth_begin "twitter" %}`, not `'twitter_app'`. That name represents the backend you intend to use. Also you should include DSA `urls.py` into your main urls with a rule like this: `('', include('social_auth.urls'))`. This setting `SOCIAL_AUTH_ENABLED_BACKENDS` doesn't exist anymore. – omab Mar 05 '13 at 14:44
  • Thanks @omab I've made these changes, and when I click on `mysite.com/twitter` it just takes me to a page as if I am logged in (even though I did not have to do anything). – djq Mar 05 '13 at 16:00
  • try to create /twitter/done/ page – catherine Mar 11 '13 at 03:34
  • I read the documentation now and there is no stated that you must create twitter url or view. You just is to put url twitter in your template – catherine Mar 12 '13 at 17:04
  • post your full urls.py, hence if the order you've posted is like the one you're using the latter will never be reached as everything matches the first rule (`url('', include('social_auth.urls')),`) for starters – Hedde van der Heide Mar 14 '13 at 21:53

1 Answers1

5

I will give you a sample and this is a customize twitter login,

  1. the name of the app is social
  2. pip install Twython
  3. create LOGIN_REDIRECT_URL, TWITTER_SECRET and TWITTER_KEY

settings.py

# Twitter settings
TWITTER_KEY = 'xxxxxx'
TWITTER_SECRET = 'xxxxxxxx'

models.py

class TwitterProfile(models.Model):
    user = models.ForeignKey(User)
    oauth_token = models.CharField(
        max_length=200
        )
    oauth_secret = models.CharField(
        max_length=200
        )
    screen_name = models.CharField(
        max_length=50, 
        blank=True, null=True
        )

    def __unicode__(self):
        return "{0}".format(self.user)

urls.py

urlpatterns = patterns('social.views',      
    url(
        r'^twitter/login/$', 
        "twitter_login", 
        name="twitter_login"
    ),

    url(r'^twitter/callback/$', 
        "twitter_callback", 
        name="twitter_callback"
    ),   
)

views.py

def twitter_login(request):
    twitter = Twython(
        twitter_token = settings.TWITTER_KEY,
        twitter_secret = settings.TWITTER_SECRET,
        callback_url = request.build_absolute_uri(reverse('social:twitter_callback'))
    )
    auth_props = twitter.get_authentication_tokens()
    request.session['request_token'] = auth_props
    return HttpResponseRedirect(auth_props['auth_url'])


def twitter_callback(request, redirect_url=settings.LOGIN_REDIRECT_URL):
    twitter = Twython(
        twitter_token = settings.TWITTER_KEY,
        twitter_secret = settings.TWITTER_SECRET,
        oauth_token = request.session['request_token']['oauth_token'],
        oauth_token_secret = request.session['request_token']['oauth_token_secret'],
    )

    authorized_tokens = twitter.get_authorized_tokens()

    try:
        profile = TwitterProfile.objects.get(screen_name = authorized_tokens['screen_name'])
        user = User.objects.get(pk=profile.user_id)
        user.backend = 'django.contrib.auth.backends.ModelBackend'

        if user.is_active:
            auth_login(request, user)
            return HttpResponseRedirect(reverse('app_name:url_name'))
        else:
            //failed back to login
            return HttpResponseRedirect(reverse('app_name:login'))
    except TwitterProfile.DoesNotExist:
        screen_name = authorized_tokens['screen_name']
        oauth_token = authorized_tokens['oauth_token']
        oauth_token_secret = authorized_tokens['oauth_token_secret']

        //create new twitter profile
        //create new user here
        //authenticate the new register user then login
        .........

template

<a href="{% url social:twitter_login %}">Twitter</a>
catherine
  • 21,020
  • 12
  • 54
  • 75
  • Thanks for the example. Do you know if the `views.py` aspect would be similar with `django-socialauth`? – djq Mar 05 '13 at 13:02
  • yeah that's the origin but I customize it a little because it difficult to follow and use third party app – catherine Mar 05 '13 at 13:04
  • ok - I'll test it out. Were these the only changes to `settings.py`the twitter keys? Also what did you add to `urls.py`? – djq Mar 05 '13 at 13:14
  • twitter keys and secret. I forgot the urls.py codes sorry. I will update it right away – catherine Mar 05 '13 at 13:17