Dropbox API请求令牌无法在Python 3中使用? [英] Dropbox API request token not working with Python 3?
问题描述
我正在使用官方的Dropbox API维护Python应用程序。为了要求用户让我的应用程序使用其Dropbox帐户,我使用了一个使用DropboxSession类的小脚本,该脚本显然与我们在此博客文章:
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
虽然可以与Python 2.7.6完美配合,但似乎由于Python 3.4中的Dropbox代码而失败(已处理了raw_input问题)。我收到此错误:
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.
长话短说,在研究了错误的代码之后,似乎Dropbox代码正在搜索字符串字典键,尽管事实上在Python 3中,这些键仍成为字节串(即它查找'oauth_token'
,而不是这里的 b'oauth_token'
,在这里)。
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'
所以错误的函数是 sess.obtain_request_token()
和 sess.obtain_access_token(request_token)
。而且Python 2.7版本可以正常工作,但是我想保持Python 3的兼容性。
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.
那么,有人知道如何使它在Python 3中工作吗?为了使人们继续使用新程序,是否可以故意破坏它?早些时候我可能发誓它正在与Python 3一起使用。
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 :)
编辑:似乎Dropbox SDK尚未完全与Python 3兼容。因此,我想除了等待他们更新SDK外,别无他法。
推荐答案
比等待SDK兼容,您可以使用(或贡献并使用)社区分支, dropbox-py3 (在github上)。
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).
(那些引号是大引号。目前仅是我编写此代码,这是我需要的部分,但是欢迎大家提供帮助。我认为这主要是确定少数部分缺少 .encode,因为它混合了字节和字符串。)
(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.)
这篇关于Dropbox API请求令牌无法在Python 3中使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!