2013-10-06 102 views
2

我在我的Django v1.5.3應用程序中使用DropboxOAuth2Flow方法描述Dropbox API v1.6,並且在重定向到dropbox oauth2授權頁面時出現400錯誤。使用python dropbox API和Django

當我去到我的dropbox_auth_start URL我重定向到:

https://www.dropbox.com/1/oauth2/authorize?state=tWd4Eh4nzk5NlcuHXe7ffA%3D%3D&redirect_uri=http%3A%2F%2Fmydomain.com%2Fdropbox_auth_finish&response_type=code&client_id=blahblahblah

再出現400錯誤。

順便說一下,「dropbox-auth-csrf-token」寫在會話文件中。

我的Django代碼:

views.py

def get_dropbox_auth_flow(web_app_session): 
    redirect_uri = "http://www.mydomain.com" 
    return DropboxOAuth2Flow('blahblahblah', 'blehblehbleh', redirect_uri, web_app_session, "dropbox-auth-csrf-token") 

# URL handler for /dropbox-auth-start 
def dropbox_auth_start(request): 
    authorize_url = get_dropbox_auth_flow(request.session).start() 
    return HttpResponseRedirect(authorize_url) 

# URL handler for /dropbox-auth-finish 
def dropbox_auth_finish(request): 
    try: 
     access_token, user_id, url_state = get_dropbox_auth_flow(request.session).finish(request.GET) 
    except DropboxOAuth2Flow.BadRequestException, e: 
     http_status(400) 
    except DropboxOAuth2Flow.BadStateException, e: 
     # Start the auth flow again. 
     return HttpResponseRedirect("http://www.mydomain.com/dropbox_auth_start") 
    except DropboxOAuth2Flow.CsrfException, e: 
     return HttpResponseForbidden() 
    except DropboxOAuth2Flow.NotApprovedException, e: 
     raise e 
    except DropboxOAuth2Flow.ProviderException, e: 
     raise e 

urls.py

from django.conf.urls import patterns, url, include 
from django.contrib import admin 
admin.autodiscover() 


urlpatterns = patterns('', 
    url(r'^dropbox_auth_start/?$',views.dropbox_auth_start), 
    url(r'^dropbox_auth_finish/?$',views.dropbox_auth_finish), 
) 
+3

看起來您可能正在使用HTTP? OAuth 2.0需要HTTPS重定向URI。 – smarx

+0

我剛剛爲我的網站創建了SSL證書,在views.py中添加了https,但我得到了相同的結果:( – Axel

+2

您確定在應用程序控制臺中正確設置了auth回調嗎? –

回答

1

就像@smarx說,我剛剛從HTTP和HTTPS切換,一切都只是工作精細。

1

我最近遇到了問題,我的網站鏈接始終使用https鏈接。我不確定我的解決方案是否完全有效或安全,但目前它可以阻止導致我的服務出現很多不良註冊問題的錯誤。

因爲在某些情況下,當用戶重定向到Dropbox並返回時,Django會話層看起來不起作用,似乎CSRF令牌作爲回調響應中的「狀態」參數傳回給您的應用。我的解決方案是檢查視圖處理程序中的身份驗證,以檢查csrf會話密鑰是否存在,以及它是否在調用dropbox請求身份驗證流程之前從參數「state」中將其添加到會話中。

try: 
     if request.session["dropbox-auth-csrf-token"] is None or request.session["dropbox-auth-csrf-token"] == "": 
      raise Exception("Problem with csrf") 
    except Exception, e: 
     #Get it from the parameter and add it to the session. 
     csrf = request.GET.get("state") 
     request.session["dropbox-auth-csrf-token"] = csrf 

    access_token, user_id, url_state = \ 
      get_dropbox_auth_flow(request.session).finish(request.GET) 

我不知道它是否可以添加到Django的庫Dropbox的整體修復,檢查請求參數的狀態變量會話是否出於某種原因不能正常工作。這實際上可能是一個安全問題,目前它解決了我的註冊問題。