简体   繁体   English

使用django的python dropbox API

[英]Use python dropbox API with django

I'm using DropboxOAuth2Flow method described for the dropbox API v1.6 in my Django v1.5.3 application and I'm having a 400 error when redirected to the dropbox oauth2 authorization page. 我正在使用我的Django v1.5.3应用程序中为Dropbox API v1.6描述的DropboxOAuth2Flow方法,并且在重定向到dropbox oauth2授权页面时遇到400错误。

When I go to to my dropbox_auth_start URL I get redirected to: 当我转到我的dropbox_auth_start网址时,我被重定向到:

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 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

And then the 400 error occurs. 然后发生400错误。

The "dropbox-auth-csrf-token" is written in the session file by the way. 顺便说一句,“dropbox-auth-csrf-token”会写在会话文件中。

My django code: 我的django代码:

views.py 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 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),
)

就像@smarx说的那样,我刚刚从HTTP和HTTPS切换,一切正常。

I've recently had a problem with this and my site link was always using the https link. 我最近遇到了这个问题,我的网站链接总是使用https链接。 I'm not sure if my solution is fully valid or secure, but for the moment it stops a bug that's causing a lot of bad signup problems for my service. 我不确定我的解决方案是完全有效还是安全,但目前它会阻止导致我的服务出现大量不良注册问题的错误。

Because in some cases the Django Session layer does not seem to work when users are redirected to dropbox and back it seems that the CSRF token is passed back to your app as the "state" parameter in the callback response. 因为在某些情况下,当用户被重定向到dropbox并且返回时,Django会话层似乎不起作用,似乎CSRF令牌作为回调响应中的“state”参数传递回您的应用程序。 My solution is to do a check in your view handler for the authentication that checks if the csrf session key exists and if it does not to get it from the parameter "state" and add it to the session before calling the dropbox request authentication flow. 我的解决方案是在您的视图处理程序中检查身份验证,检查csrf会话密钥是否存在,以及是否从参数“state”获取它并在调用dropbox请求身份验证流程之前将其添加到会话中。

    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)

I'm not sure if it's an overall fix that can be added to the Django library for dropbox, to check the request parameter for the state variable if the session is for some reason not working. 我不确定它是否可以添加到Django库中用于dropbox的整体修复,如果会话由于某种原因无效,则检查状态变量的请求参数。 This may in fact be a security problem, for the moment it solves my signup issues. 这实际上可能是一个安全问题,目前它解决了我的注册问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM