TwitterBackend

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python
"""Twitter Authentication backend for Django

Requires:
AUTH_PROFILE_MODULE to be defined in settings.py

The profile models should have following fields:
        access_token
        url
        location
        description
        profile_image_url
"""

from django.conf import settings
from django.contrib.auth.models import User

import oauthtwitter


CONSUMER_KEY = getattr(settings, 'CONSUMER_KEY', 'YOUR_KEY')
CONSUMER_SECRET = getattr(settings, 'CONSUMER_SECRET', 'YOUR_SECRET')



class TwitterBackend:
    """TwitterBackend for authentication
    """
    def authenticate(self, access_token):
        '''authenticates the token by requesting user information from twitter
        '''
        twitter = oauthtwitter.OAuthApi(CONSUMER_KEY, CONSUMER_SECRET, access_token)
        try:
            userinfo = twitter.GetUserInfo()
        except:
            # If we cannot get the user information, user cannot be authenticated
            return None

        screen_name = userinfo.screen_name

        user, created = User.objects.get_or_create(username=screen_name)
        if created:
            # create and set a random password so user cannot login using django built-in authentication
            temp_password = User.objects.make_random_password(length=12)
            user.set_password(temp_password)

        user.first_name = userinfo.name
        user.save()

        # Get the user profile
        userprofile = user.get_profile()
        userprofile.access_token = access_token
        userprofile.url = userinfo.url
        userprofile.location = userinfo.location
        userprofile.description = userinfo.description
        userprofile.profile_image_url = userinfo.profile_image_url
        userprofile.save()
        return user
        
    
    def get_user(self, id):
        try:
            return User.objects.get(pk=id)
        except:
            return None

More like this

  1. Permission Required Middleware by mattgrayson 4 years, 6 months ago
  2. Improved User Admin by gregb 3 years, 1 month ago
  3. Show logged users - keeping track of users login and logout by albertorcf 9 months, 2 weeks ago
  4. Twitter oAuth example by henriklied 4 years, 3 months ago
  5. Simple Paginator Function by goodsanket 3 years, 4 months ago

Comments

thomasw (on October 7, 2009):

In order to get this to work, I ended up changing line 56 from:

userprofile.access_token = access_token

to

userprofile.access_token = access_token.to_string()

This was the error: Error binding parameter 1 - probably unsupported type.

With python 2.6.3, sqlite 3.6.18, django trunk (r11600) the sqlite library was throwing an error on line 57 when it tried to save the model.

I was found the snippet linked from the django how-to at http://agileweb.wordpress.com/2009/04/28/step-by-step-guide-to-use-sign-in-with-twitter-with-django/

#

(Forgotten your password?)