3 from django.contrib.auth.decorators import login_required
4 from django.core.urlresolvers import reverse
5 from django.http import HttpResponseRedirect, HttpResponse
8 from apiclient.models import OAuthConnection
9 from apiclient import wl_consumer
10 from apiclient.settings import (WL_REQUEST_TOKEN_URL, WL_ACCESS_TOKEN_URL,
16 if wl_consumer is None:
17 return HttpResponse("OAuth consumer not configured.")
19 client = oauth2.Client(wl_consumer)
20 resp, content = client.request(WL_REQUEST_TOKEN_URL)
21 if resp['status'] != '200':
22 raise Exception("Invalid response %s." % resp['status'])
24 request_token = dict(cgi.parse_qsl(content))
26 conn = OAuthConnection.get(request.user)
27 # this might reset existing auth!
29 conn.token = request_token['oauth_token']
30 conn.token_secret = request_token['oauth_token_secret']
33 url = "%s?oauth_token=%s&oauth_callback=%s" % (
35 request_token['oauth_token'],
36 request.build_absolute_uri(reverse("users_oauth_callback")),
39 return HttpResponseRedirect(url)
43 def oauth_callback(request):
44 if wl_consumer is None:
45 return HttpResponse("OAuth consumer not configured.")
47 oauth_verifier = request.GET.get('oauth_verifier')
48 conn = OAuthConnection.get(request.user)
49 token = oauth2.Token(conn.token, conn.token_secret)
50 token.set_verifier(oauth_verifier)
51 client = oauth2.Client(wl_consumer, token)
52 resp, content = client.request(WL_ACCESS_TOKEN_URL, method="POST")
53 access_token = dict(cgi.parse_qsl(content))
56 conn.token = access_token['oauth_token']
57 conn.token_secret = access_token['oauth_token_secret']
60 return HttpResponseRedirect('/')