2

使用 OAuth 身份验证方法时,我无法从 url 检索身份验证代码。谷歌将我重定向到一个附有代码的网址

..我认为我在正则表达式中犯了一个错误,因为在验证自己并授予应用程序管理我的日历的权限后,我被重定向到 http://127.0.0.1/Main/addloanpremium/?code=4/2wi1hu0Gv8YZuKo79kc-kjCmw7qj0W2EyLYa3qzIe7w # ..如何从 url 中提取代码..上面的 url 在新选项卡上打开,由于我的 urls.py 显示相同的表单页面,但是当我尝试使用 request.GET 访问“代码”值时。 get('code') 它没有给出任何值..

异常值:无法将“NoneType”对象隐式转换为 str

我的views.py 看起来像:

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',{})

我的主要:urls.py:

url(r'^addloanpremium/$',views.addloanpremium,name='addloanpremium'),

我的 loan_pre_form.html 包含

<form action=" {% url 'Main:addloanpremium' %}" method="GET">

任何帮助将不胜感激。

4

1 回答 1

0

根据这个SO answer,函数返回一个值,如果您不指定一个值,它将隐式返回None异常值:无法将 'NoneType' 对象隐式转换为 str错误意味着您尝试将 aNoneType用作字符串。我想问题是这个函数没有返回任何东西(在一种情况下),所以它实际上返回了NoneType.

这是来自 Google 文档的示例。

您还可以检查这些相关线程:

希望这可以帮助!

于 2016-09-28T15:36:48.777 回答