2

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.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
Gautam Worah
  • 19
  • 1
  • 3

1 Answers1

0

Based from this SO answer, functions return a value and if you don't specify one, it will implicitly return None. Exception Value: Can't convert 'NoneType' object to str implicitly error means that you try to use a NoneType as a string. I guess the problem is that this function doesn't return anything (in one case) so it actually returns NoneType.

Here is an example from Google documentation.

You can also check on these related threads:

Hope this helps!

Community
  • 1
  • 1
abielita
  • 13,147
  • 2
  • 17
  • 59