def oauth(request): if wl_consumer is None: return HttpResponse("OAuth consumer not configured.") client = oauth2.Client(wl_consumer) resp, content = client.request(WL_REQUEST_TOKEN_URL) if resp['status'] != '200': raise Exception("Invalid response %s." % resp['status']) request_token = dict(cgi.parse_qsl(content)) conn = OAuthConnection.get(request.user) # this might reset existing auth! conn.access = False conn.token = request_token['oauth_token'] conn.token_secret = request_token['oauth_token_secret'] conn.save() url = "%s?oauth_token=%s&oauth_callback=%s" % ( WL_AUTHORIZE_URL, request_token['oauth_token'], request.build_absolute_uri(reverse("apiclient_oauth_callback")), ) return HttpResponseRedirect(url)
def api_call(user, path, data=None): conn = OAuthConnection.get(user) if not conn.access: raise NotAuthorizedError("No WL authorization for user %s." % user) token = oauth2.Token(conn.token, conn.token_secret) client = oauth2.Client(wl_consumer, token) if data is not None: data = simplejson.dumps(data) data = urllib.urlencode({"data": data}) resp, content = client.request( "%s%s" % (WL_API_URL, path), method="POST", body=data) else: resp, content = client.request( "%s%s" % (WL_API_URL, path)) status = resp['status'] if status == '200': return simplejson.loads(content) elif status.startswith('2'): return elif status == '401': raise ApiError('User not authorized for publishing.') else: raise ApiError("WL API call error [code %s]" % status)
def oauth(request): oauth = OAuth1Session(WL_CONSUMER_KEY, WL_CONSUMER_SECRET) request_token = oauth.fetch_request_token(WL_REQUEST_TOKEN_URL) conn = OAuthConnection.get(request.user) conn.access = False conn.token = request_token['oauth_token'] conn.token_secret = request_token['oauth_token_secret'] conn.save() url = oauth.authorization_url(WL_AUTHORIZE_URL) url += '&oauth_callback=' + request.build_absolute_uri(reverse("apiclient_oauth_callback")) return HttpResponseRedirect(url)
def oauth_callback(request): conn = OAuthConnection.get(request.user) oauth_verifier = request.GET.get('oauth_verifier', 'verifier') oauth = OAuth1Session( WL_CONSUMER_KEY, WL_CONSUMER_SECRET, conn.token, conn.token_secret, verifier=oauth_verifier) access_token = oauth.fetch_access_token(WL_ACCESS_TOKEN_URL) conn.access = True conn.token = access_token['oauth_token'] conn.token_secret = access_token['oauth_token_secret'] conn.save() return HttpResponseRedirect('/')
def oauth_callback(request): if wl_consumer is None: return HttpResponse("OAuth consumer not configured.") oauth_verifier = request.GET.get('oauth_verifier') conn = OAuthConnection.get(request.user) token = oauth2.Token(conn.token, conn.token_secret) 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() return HttpResponseRedirect('/')