Dropbox API请求令牌不能与Python 3一起使用?

时间:2014-09-12 16:45:32

标签: python python-2.7 python-3.x dropbox dropbox-api

我正在使用官方Dropbox API维护Python应用程序。要让用户让我的应用程序使用他们的Dropbox帐户,我使用DropboxSession类使用一个小脚本,这与我们在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

虽然它完全适用于Python 2.7.6,但由于Python 3.4中的Dropbox代码(已经处理了raw_input问题),它似乎失败了。我收到这个错误:

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.

长话短说,在研究了错误的代码后,似乎Dropbox代码搜索字符串字典键,尽管在Python 3中,这些键变为字节串(即它查找'oauth_token',不在这里,而不是b'oauth_token',这是在这里。

然而,即使修复了代码以确定这是唯一的问题,也没有运气,我在程序中又得到了另一个错误:

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'

错误的功能是sess.obtain_request_token()sess.obtain_access_token(request_token)。 Python 2.7版本运行良好,但我希望保持Python 3的兼容性。

那么,是否有人知道如何让它在Python 3中运行?为了让人们接受新的程序,是否可以故意破坏?我原本可以发誓它正在使用Python 3。

感谢您的时间,如果您有任何想法:)

编辑:Dropbox SDK似乎还不完全兼容Python 3。所以,我想除了等待他们更新SDK之外别无他法。

2 个答案:

答案 0 :(得分:0)

尝试使用版本1.6

$ pip install dropbox==1.6

答案 1 :(得分:0)

比等待SDK兼容更好,您可以使用(或贡献并使用)“社区”分支,dropbox-py3here on github)。

(那些引用是很大的引用。现在只是我编码这个,只是我需要的部分,但是每个人都欢迎提供帮助。我认为它主要是识别缺少“.encode”的少数部分,因为它混合了字节和字符串。)

相关问题