While using the OAuth authentication method I am unable to retrieve the authentication code from the url. Google redirects me to a url with code appended to its end
..I think I am making a mistake in the regex because after authenticating myself and giving the app the permission to manage my calendar I am redirected to http://127.0.0.1/Main/addloanpremium/?code=4/2wi1hu0Gv8YZuKo79kc-kjCmw7qj0W2EyLYa3qzIe7w# ..How do I extract the code from the url..The above url opens on a new tab and due to my urls.py displays the same form page however when I try to access the 'code' value using request.GET.get('code') it does not give any value..
Exception Value: Can't convert 'NoneType' object to str implicitly
My views.py looks like:
def addloanpremium(request):
if request.method == 'GET':
loan=Loan()
premium=Premium()
user=request.user
#form=loan_pre_form(request.POST)
if(request.GET.get('typelp')=='loan'):
loan.user=user
#premium.user=user
#lots of code here..all values from fields are stored into the db
if(request.GET.get("iscalendar")=="True"):
print(" \n \n entered iscalendar =true \n")
print(" \n \n entered calendar now trying to add event \n \n")
SCOPES = 'https://www.googleapis.com/auth/calendar'
flow=auth2client.client.flow_from_clientsecrets('C:/Users/ghw/Desktop/Expenze/Main/client_secrets.json',SCOPES,redirect_uri='http://127.0.0.1:8000/Main/addloanpremium/')
storage = oauth2client.file.Storage('credentials.dat')
credentials = storage.get()
if not credentials or credentials.invalid:
auth_uri = flow.step1_get_authorize_url()
print("\n value of auth uri is "+auth_uri+"\n")
webbrowser.open(auth_uri)
auth_code = request.GET.get('code')
print("\n value of auth code is "+auth_code+"\n")
### I am getting the auth code wrong
credentials = flow.step2_exchange(auth_code)
storage.put(credentials)
http = httplib2.Http()
http = credentials.authorize(http)
CAL = build('calendar', 'v3', http=http)
.
.#lots of code here
.
return render(request,'Main/loan_pre_form.html',{})
my Main:urls.py:
url(r'^addloanpremium/$',views.addloanpremium,name='addloanpremium'),
my loan_pre_form.html contains
<form action=" {% url 'Main:addloanpremium' %}" method="GET">
Any help would be highly appreciated.