title: Use python dropbox API with django
body: <p>I'm using <a href="https://www.dropbox.com/static/developers/dropbox-python-sdk-1.6-docs/index.html#dropbox.client.DropboxOAuth2Flow" rel="nofollow">DropboxOAuth2Flow</a> 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.</p>
  
  <p>When I go to to my dropbox_auth_start URL I get redirected to:</p>
  
  <p><a href="https://www.dropbox.com/1/oauth2/authorize?state=tWd4Eh4nzk5NlcuHXe7ffA%3D%3D&amp;redirect_uri=http%3A%2F%2Fmydomain.com%2Fdropbox_auth_finish&amp;response_type=code&amp;client_id=blahblahblah" rel="nofollow">https://www.dropbox.com/1/oauth2/authorize?state=tWd4Eh4nzk5NlcuHXe7ffA%3D%3D&amp;redirect_uri=http%3A%2F%2Fmydomain.com%2Fdropbox_auth_finish&amp;response_type=code&amp;client_id=blahblahblah</a></p>
  
  <p>And then the 400 error occurs.</p>
  
  <p>The "dropbox-auth-csrf-token" is written in the session file by the way.</p>
  
  <p>My django code:</p>
  
  <p>views.py</p>
  
  <pre><code>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
  </code></pre>
  
  <p>urls.py</p>
  
  <pre><code>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),
  )
  </code></pre>