-import cgi
+# -*- coding: utf-8 -*-
+import urlparse
from django.contrib.auth.decorators import login_required
from django.core.urlresolvers import reverse
from apiclient.models import OAuthConnection
from apiclient import wl_consumer
-from apiclient.settings import (WL_REQUEST_TOKEN_URL, WL_ACCESS_TOKEN_URL,
- WL_AUTHORIZE_URL)
+from apiclient.settings import (WL_REQUEST_TOKEN_URL, WL_ACCESS_TOKEN_URL, WL_AUTHORIZE_URL)
@login_required
if resp['status'] != '200':
raise Exception("Invalid response %s." % resp['status'])
- request_token = dict(cgi.parse_qsl(content))
-
+ request_token = dict(urlparse.parse_qsl(content))
+
conn = OAuthConnection.get(request.user)
# this might reset existing auth!
conn.access = False
conn.save()
url = "%s?oauth_token=%s&oauth_callback=%s" % (
- WL_AUTHORIZE_URL,
+ WL_AUTHORIZE_URL,
request_token['oauth_token'],
- request.build_absolute_uri(reverse("users_oauth_callback")),
+ request.build_absolute_uri(reverse("apiclient_oauth_callback")),
)
return HttpResponseRedirect(url)
token.set_verifier(oauth_verifier)
client = oauth2.Client(wl_consumer, token)
resp, content = client.request(WL_ACCESS_TOKEN_URL, method="POST")
- access_token = dict(cgi.parse_qsl(content))
-
- conn.access = True
- conn.token = access_token['oauth_token']
- conn.token_secret = access_token['oauth_token_secret']
- conn.save()
+ access_token = dict(urlparse.parse_qsl(content))
+
+ if 'oauth_token' in access_token:
+ conn.access = True
+ conn.token = access_token['oauth_token']
+ conn.token_secret = access_token['oauth_token_secret']
+ conn.save()
+ else:
+ return HttpResponse(request.GET.get('error'))
return HttpResponseRedirect('/')