X-Git-Url: https://git.mdrn.pl/redakcja.git/blobdiff_plain/1b718c10066557540770bb0960a773dce0ad4462..1c4c468783e5f380324c29ebc3b2c452da8cc2a0:/apps/apiclient/views.py?ds=inline diff --git a/apps/apiclient/views.py b/apps/apiclient/views.py index d4960148..0dbaf5ac 100644 --- a/apps/apiclient/views.py +++ b/apps/apiclient/views.py @@ -1,4 +1,5 @@ -import cgi +# -*- coding: utf-8 -*- +import urlparse from django.contrib.auth.decorators import login_required from django.core.urlresolvers import reverse @@ -7,8 +8,7 @@ import oauth2 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 @@ -21,8 +21,8 @@ def oauth(request): 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 @@ -31,7 +31,7 @@ def oauth(request): 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("apiclient_oauth_callback")), ) @@ -50,11 +50,14 @@ def oauth_callback(request): 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('/')