forked from henriklied/django-twitter-oauth
-
Notifications
You must be signed in to change notification settings - Fork 2
/
views.py
57 lines (47 loc) · 2.11 KB
/
views.py
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
import oauth, time, datetime
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseRedirect, HttpResponse
from django.shortcuts import render_to_response
from django.core.urlresolvers import reverse
from twitter_auth.util.twitterutils import *
from django.conf import settings
TWITTER_AUTH_HOME = getattr(settings, 'TWITTER_AUTH_HOME', 'twitter_oauth_main')
TWITTER_AUTH_AUTHENTICATED = getattr(settings, 'TWITTER_AUTH_AUTHENTICATED', 'twitter_oauth_friend_list')
def main(request):
if request.session.has_key('access_token'):
return HttpResponseRedirect(reverse(TWITTER_AUTH_AUTHENTICATED))
else:
return render_to_response('twitter_auth/base.html')
def logout_(request):
logout(request)
request.session.clear()
return HttpResponseRedirect(reverse(TWITTER_AUTH_HOME))
def login_(request):
"/auth/"
token = get_unauthorised_request_token()
auth_url = get_authorisation_url(token)
response = HttpResponseRedirect(auth_url)
request.session['unauthed_token'] = token.to_string()
return response
def return_(request):
"/return/"
unauthed_token = request.session.get('unauthed_token', None)
if not unauthed_token:
return HttpResponse("No un-authed token cookie")
token = get_oauth_token_from_string(unauthed_token)
if token.key != request.GET.get('oauth_token', 'no-token'):
return HttpResponse("Something went wrong! Tokens do not match")
verifier = request.GET.get('oauth_verifier')
access_token = exchange_request_token_for_access_token(token, params={'oauth_verifier':verifier})
user = authenticate(oauth_access_token=access_token)
if user is not None:
login(request, user)
else:
return HttpResponse("Something went wrong! Could not log you in")
response = HttpResponseRedirect(reverse(TWITTER_AUTH_AUTHENTICATED))
return response
def friend_list(request):
api = get_twitter_api_from_request(request)
users = api.GetFriends()
return render_to_response('twitter_auth/list.html', {'users': users})