我在维护一个使用官方Dropbox API的Python应用程序。为了让用户能够让我的应用程序中使用他们的Dropbox帐户,我用用DropboxSession类,这显然是一样的一个,我们可以找到this blog post一个小脚本:Dropbox API请求令牌不适用于Python 3?
# 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之外没有别的可做的事情了。
很好的工作发布您的API密钥和世界的秘密...... – MattDMo 2014-09-12 17:01:09
Dropbox for Python SDK尚未更新为Python 3支持(截至当前版本2.1.0: https://www.dropbox.com/developers/core/sdks/python)。而且它并没有故意瘫痪。它确实使用最新版本的Dropbox API和OAuth。至于在这里结束的401错误,如果没有更多信息,这将很难调试。这可能是一个有效的错误,即您的访问令牌无效,或者可能是由于Python 3不兼容导致格式错误的请求。我建议自己查看请求,看看它可能出错的地方。 – Greg 2014-09-12 17:25:35
@MattDMo,感谢您的关注,但应用程序的关键/秘密是我把链接的博客帖子。我认为,自作者离开他们后,可以复制:-)格雷格似乎有正确的答案。因为我认为这个问题不会得到更好的结果,所以我会编辑这个问题并让它在这里作为参考。 – Scylardor 2014-09-13 20:57:43