2

使用OAuth身份驗證方法時,我無法從url中檢索身份驗證代碼。谷歌重定向我的代碼的URL附加到其結束Django Google API Client Error..Trying實現應用程序,在認證後向用戶的Google日曆中添加事件

..我想我是因爲自己的身份驗證,並給予應用程序來管理我的日曆我重定向到 http://127.0.0.1/Main/addloanpremium/?code=4/2wi1hu0Gv8YZuKo79kc-kjCmw7qj0W2EyLYa3qzIe7w#許可後進行的正則表達式錯誤..我如何從網址中提取代碼..上面的網址在新標籤頁上打開,並且由於我的urls.py顯示了相同的表單頁面,但是當我嘗試訪問使用request.GET.get('代碼「),它並沒有給任何價值..

異常值:無法將‘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"> 

任何幫助將高度讚賞。

回答

0

根據此SO answer,函數返回一個值,如果不指定一個值,它將隱式返回None異常值:無法將「NoneType」對象隱式轉換爲str錯誤表示您嘗試使用NoneType作爲字符串。我想問題是這個函數不會返回任何東西(在一種情況下),所以它實際上返回NoneType

這是Google文檔中的example

您還可以查看這些相關的主題:

希望這有助於!

相關問題