Django Twitter OAuth authentication
I'm trying to implement a Twitter authentication via OAuth on my Django application, but I'm stuck at this error after receiving the Twit开发者_Go百科ter callback:
Traceback:
File "/Library/Python/2.6/site-packages/django/core/handlers/base.py" in get_response
100. response = callback(request, *callback_args, **callback_kwargs)
File "/Users/flavioramos/projects/sobo/sobo/../sobo/views.py" in twitter_authenticated
105. auth_login(request, user)
File "/Library/Python/2.6/site-packages/django/contrib/auth/__init__.py" in login
71. user.save()
File "/Library/Python/2.6/site-packages/django/contrib/auth/models.py" in save
430. raise NotImplementedError
Exception Type: NotImplementedError at /login/authenticated
Exception Value:
My login/authenticated view is the following:
def twitter_authenticated(request):
token = oauth.Token(request.session['request_token']['oauth_token'],
request.session['request_token']['oauth_token_secret'])
client = oauth.Client(consumer, token)
resp, content = client.request(access_token_url, "GET")
if resp['status'] != '200':
print content
raise Exception("Invalid response from Twitter.")
access_token = dict(cgi.parse_qsl(content))
try:
user = User.objects.get(username=access_token['screen_name'])
except User.DoesNotExist:
user = User.objects.create_user(access_token['screen_name'],
'%s@twitter.com' % access_token['screen_name'],
access_token['oauth_token_secret'])
profile = Profile()
profile.user = user
profile.oauth_token = access_token['oauth_token']
profile.oauth_secret = access_token['oauth_token_secret']
profile.save()
user = authenticate(username=access_token['screen_name'],password=access_token['oauth_token_secret'])
auth_login(request, user)
return HttpResponseRedirect('/')
This code is from a python-oauth2 sample application.
Do I have to write my own authentication backend for this?
I'm new at Django and any help will be appreciated.
Thanks,
I had this same issue. Seems that the libs used by oauthtwitter have changed. To patch your local version do the following:
in:
/usr/local/lib/python2.6/dist-packages/oauth_python_twitter-1.0-py2.6.egg/oauthtwitter.py
find line 37 and change it from:
Api.__init__(self, access_token.key, access_token.secret)
to:
Api.__init__(self, consumer_key, consumer_secret, access_token.key, access_token.secret)
Now all the required variables are passed correctly.
Not quite sure, but I think
*auth_login(request, user)*
Should be: login(request, user)
(and ofcourse this login should be imported from django.contib.auth on top of your login/authenticated view )
精彩评论