問題描述
在 Python 中使用 OAuth2 連接到保管箱時出錯 (Error using OAuth2 to connect to dropbox in Python)
在運行 raspbian jessie 的 Raspberry Pi 上,我嘗試通過 OAuth2 流程使用我通過 pip 安裝的用於 Python 的 Dropbox SDK 將程序連接到我的 Dropbox。
為了測試,我從 documentation(當然還定義了 app‑key 和 secret):
from dropbox import DropboxOAuth2FlowNoRedirect
auth_flow = DropboxOAuth2FlowNoRedirect(APP_KEY, APP_SECRET)
authorize_url = auth_flow.start()
print "1. Go to: " + authorize_url
print "2. Click \"Allow\" (you might have to log in first)."
print "3. Copy the authorization code."
auth_code = raw_input("Enter the authorization code here: ").strip()
try:
access_token, user_id = auth_flow.finish(auth_code)
except Exception, e:
print('Error: %s' % (e,))
return
dbx = Dropbox(access_token)
我能夠獲取 URL 並點擊允許。然而,當我輸入授權碼時,它打印了以下錯誤:
Error: 'str' object has no attribute 'copy'
使用追溯模塊中的 format_exc,我得到以下信息:
Traceback (most recent call last):
File "test.py", line 18, in <module>
access_token, user_id = auth_flow.finish(auth_code)
File "/usr/local/lib/python2.7/dist‑packages/dropbox/oauth.py", line 180, in finish
return self._finish(code, None)
File "/usr/local/lib/python2.7/dist‑packages/dropbox/oauth.py", line 50, in _finish
url = self.build_url(Dropbox.HOST_API, '/oauth2/token')
File "/usr/local/lib/python2.7/dist‑packages/dropbox/oauth.py", line 111, in build_url
return "https://%s%s" % (self._host, self.build_path(target, params))
File "/usr/local/lib/python2.7/dist‑packages/dropbox/oauth.py", line 89, in build_path
params = params.copy()
AttributeError: 'str' object has no attribute 'copy'
似乎是 build_path 方法需要一個 dict 'params' 而是接收一個字符串。有什麼想法嗎?
參考解法
方法 1:
Thanks to smarx for his comment. The error is a known issue and will be fixed in version 3.42 of the SDK. source
(by Andy_Capone、Andy_Capone)