1 from django.contrib.auth.decorators import login_required
2 from django.urls import reverse
3 from django.http import HttpResponseRedirect, HttpResponse
5 from requests_oauthlib import OAuth1Session
7 from apiclient.models import OAuthConnection
8 from apiclient.settings import WL_CONSUMER_KEY, WL_CONSUMER_SECRET
9 from apiclient.settings import WL_REQUEST_TOKEN_URL, WL_ACCESS_TOKEN_URL, WL_AUTHORIZE_URL
14 oauth = OAuth1Session(WL_CONSUMER_KEY, WL_CONSUMER_SECRET)
15 request_token = oauth.fetch_request_token(WL_REQUEST_TOKEN_URL)
17 conn = OAuthConnection.get(request.user)
19 conn.token = request_token['oauth_token']
20 conn.token_secret = request_token['oauth_token_secret']
23 url = oauth.authorization_url(WL_AUTHORIZE_URL)
24 url += '&oauth_callback=' + request.build_absolute_uri(reverse("apiclient_oauth_callback"))
25 return HttpResponseRedirect(url)
29 def oauth_callback(request):
30 conn = OAuthConnection.get(request.user)
31 oauth_verifier = request.GET.get('oauth_verifier', 'verifier')
33 oauth = OAuth1Session(
34 WL_CONSUMER_KEY, WL_CONSUMER_SECRET,
35 conn.token, conn.token_secret,
36 verifier=oauth_verifier)
37 access_token = oauth.fetch_access_token(WL_ACCESS_TOKEN_URL)
40 conn.token = access_token['oauth_token']
41 conn.token_secret = access_token['oauth_token_secret']
44 return HttpResponseRedirect('/')