Dropbox API请求令牌在Python 3中无法使用?
我正在维护一个使用官方Dropbox API的Python应用程序。为了让用户允许我的应用使用他们的Dropbox账户,我用一个小脚本,利用了DropboxSession类,这个类在这篇博客中也有介绍:
# 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中就出问题了(虽然处理了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 个回答
与其等着SDK兼容,不如直接使用(或者参与贡献并使用)这个“社区”版本,dropbox-py3(在GitHub上可以找到)。
(这些引号是很重要的。目前只有我在编写这个,主要是我需要的部分,但欢迎大家来帮忙。我觉得主要是找出那些缺少“.encode”的地方,因为它在处理字节和字符串时混淆了。)
试着使用1.6版本
$ pip install dropbox==1.6