1

I'm maintaining a Python application using the official Dropbox API. To ask the users to let my application use their Dropbox account, I use a small script using the DropboxSession class, which is clearly the same as the one we can find on this blog post :

# Include the Dropbox SDK libraries
from dropbox import client, rest, session

# Get your app key and secret from the Dropbox developer website
APP_KEY = '******'
APP_SECRET = '******'

# ACCESS_TYPE should be 'dropbox' or 'app_folder' as configured for your app
ACCESS_TYPE = 'app_folder'

sess = session.DropboxSession(APP_KEY, APP_SECRET, ACCESS_TYPE)
request_token = sess.obtain_request_token()
url = sess.build_authorize_url(request_token)

# Make the user sign in and authorize this token
print "url:", url
print "Please visit this website and press the 'Allow' button, then hit 'Enter' here."
# Python 2/3 compatibility
try:
    raw_input()
except NameError:
    input()
# This will fail if the user didn't visit the above URL
access_token = sess.obtain_access_token(request_token)

#Print the token for future reference
print access_token

While it's perfectly working with Python 2.7.6, it seems to fail because of Dropbox code in Python 3.4 (the raw_input problem having been dealt with). I get this error :

Traceback (most recent call last):
  File "/home/scylardor/.virtualenvs/onitu3/lib/python3.4/site-packages/dropbox/session.py", line 285, in _parse_token
    key = params['oauth_token'][0]
KeyError: 'oauth_token'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "get_access_token.py", line 12, in <module>
    request_token = sess.obtain_request_token()
  File "/home/scylardor/.virtualenvs/onitu3/lib/python3.4/site-packages/dropbox/session.py", line 185, in obtain_request_token
    self.request_token = self._parse_token(response.read())
  File "/home/scylardor/.virtualenvs/onitu3/lib/python3.4/site-packages/dropbox/session.py", line 287, in _parse_token
    raise ValueError("'oauth_token' not found in OAuth request.")
ValueError: 'oauth_token' not found in OAuth request.

Long story short, after having studied the faulty code, it seems that the Dropbox code searches for a string dictionary key, despite the fact that in Python 3, those keys become bytestrings (i.e. it lookups 'oauth_token', which isn't here, instead of b'oauth_token', which is here).

However, even after having fixed the code to see if that's the only issue, no luck, I get another error further in the procedure:

Traceback (most recent call last):
  File "get_access_token.py", line 25, in <module>
    access_token = sess.obtain_access_token(request_token)
  File "/home/scylardor/.virtualenvs/onitu3/lib/python3.4/site-packages/dropbox/session.py", line 214, in obtain_access_token
    response = self.rest_client.POST(url, headers=headers, params=params, raw_response=True)
  File "/home/scylardor/.virtualenvs/onitu3/lib/python3.4/site-packages/dropbox/rest.py", line 316, in POST
    return cls.IMPL.POST(*n, **kw)
  File "/home/scylardor/.virtualenvs/onitu3/lib/python3.4/site-packages/dropbox/rest.py", line 254, in POST
    post_params=params, headers=headers, raw_response=raw_response)
  File "/home/scylardor/.virtualenvs/onitu3/lib/python3.4/site-packages/dropbox/rest.py", line 227, in request
    raise ErrorResponse(r, r.read())
dropbox.rest.ErrorResponse: [401] 'Unauthorized'

So the faulty functions are sess.obtain_request_token() and sess.obtain_access_token(request_token). And the Python 2.7 version works fine, but I'd like to keep Python 3 compatibility.

So, does anyone know how one's supposed to make it work in Python 3 ? Could it be deliberately broken in order to make people move on to new procedures ? I could have sworn it was working with Python 3, some time ago.

Thank you for your time if you have an idea :)

edit: It seems the Dropbox SDK just isn't fully Python 3-compatible yet. So, I guess there's nothing else to do than to wait for them to update the SDK.

3
  • 1
    nice job publishing your API key and secret to the world... Commented Sep 12, 2014 at 17:01
  • 1
    The Dropbox for Python SDK hasn't yet been updated for Python 3 support (as of the current version, 2.1.0: dropbox.com/developers/core/sdks/python ). And it hasn't intentionally been crippled. It does use latest versions of the Dropbox API and OAuth anyway. As for the 401 error you're getting at the end here, this will be hard to debug without more information. It may be a valid error, i.e., your access token is invalid, or may be due to a malformed request because of the Python 3 incompatibility. I recommend looking at the request itself to see where it may have gone wrong. Commented Sep 12, 2014 at 17:25
  • @MattDMo , thank you for your concern, but the app key/secret was the ones of the blog post I put the link of. I assumed that since the author left them there, it was okay to copy :-) Greg seems to have the correct answer. Since I think the question won't get a better one, I'll edit the question and let it here for reference. Commented Sep 13, 2014 at 20:57

2 Answers 2

0

Try to use version 1.6

$ pip install dropbox==1.6
Sign up to request clarification or add additional context in comments.

Comments

0

Better than waiting for the SDK to be compatible, you can use (or contribute to and use) the "community" fork, dropbox-py3 (here on github).

(Those quotes are big quotes. For now it's just me coding this, and just the part I need, but everyone's welcome to help. I think it's mainly identifying the few parts that are missing a ".encode" because it's mixing bytes and strings.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.