我正在學習如何使用Dropbox Python SDK。但是,當我嘗試生成我的請求令牌時遇到問題。這裏是我使用的代碼(注意,我已經取代了我的實際應用程序鍵和祕密與APP_KEY和APP_SECRET這裏,但我用我的實際應用程序鍵和祕密,當我嘗試一下。)Dropbox Python SDK SSL錯誤
from dropbox import client, rest, session
APP_KEY = 'APP_KEY'
APP_SECRET = 'APP_SECRET'
ACCESS_TYPE = 'app_folder'
print 'Creating session object'
sess = session.DropboxSession(APP_KEY, APP_SECRET, ACCESS_TYPE)
print 'Session created!\nCreating request token'
request_token = sess.obtain_request_token()
print 'Created request token!'
url = sess.build_authorize_url(request_token)
print url
raw_input()
access_token = sess.obtain_access_token(request_token)
client1 = client.DropboxClient(sess)
print client1.account_info()
我該程序會在創建不同對象時打印出消息,以便我可以查看發生錯誤的位置。這是輸出:
[email protected]:~/Twisted$ python example.py
Creating session object
Session created!
Creating request token
Traceback (most recent call last):
File "example.py", line 8, in <module>
request_token = sess.obtain_request_token()
File "/usr/local/lib/python2.7/dist-packages/dropbox-1.4-py2.7.egg/dropbox/session.py", line 160, in obtain_request_token
response = rest.RESTClient.POST(url, headers=headers, params=params, raw_response=True)
File "/usr/local/lib/python2.7/dist-packages/dropbox-1.4-py2.7.egg/dropbox/rest.py", line 140, in POST
return cls.request("POST", url, post_params=params, headers=headers, raw_response=raw_response)
File "/usr/local/lib/python2.7/dist-packages/dropbox-1.4-py2.7.egg/dropbox/rest.py", line 64, in request
conn = ProperHTTPSConnection(host, 443)
File "/usr/local/lib/python2.7/dist-packages/dropbox-1.4-py2.7.egg/dropbox/rest.py", line 214, in __init__
self.cert_reqs = ssl.CERT_REQUIRED
AttributeError: 'module' object has no attribute 'CERT_REQUIRED'
我曾嘗試使用此代碼,並沒有遇到此問題。我也刪除了Dropbox SDK並重新安裝,結果沒有。什麼導致這個問題,我該如何解決它?
UPDATE
添加卡納安的代碼後,輸出結果如下: Ready
Generating session
Session Generated!
Generating request token
['Certificate', 'CertificateOptions', 'CertificateRequest', 'Client', 'ClientContextFactory', 'Connector', 'ContextFactory', 'DN', 'DefaultOpenSSLContextFactory', 'DistinguishedName', 'KeyPair', 'Port', 'PrivateCertificate', 'SSL', 'Server', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', 'implementedBy', 'implements', 'implementsOnly', 'interfaces', 'supported', 'tcp']
然後我得到的錯誤。我嘗試重新創建我的SSL證書,但沒有運氣。
你也做'打印SSL .__文件___'?從'dir(ssl)'結果看來,你看起來像Twisted的twisted.internet.ssl而不是標準的'ssl'。也許你的進口路線搞砸了? –
你是對的! ssl .__ file__輸出/usr/lib/python2.7/dist-packages/twisted/internet/ssl.pyc。我忘記了我導入了* - ** twisted.internet.ssl **,這樣我就可以在我的客戶端和服務器程序之間建立安全連接。我嘗試將twisted聲明更改爲**從twisted.internet import ssl twisted_ssl **,以便它不會干擾Dropbox,並在** reactor.listenSSL **中更新對** ssl **的引用。不過,我仍然有ssl.CERT_REQUIRED錯誤。 –
Dropbox SDK不會導入ssl,所以我不知道爲什麼'twisted.internet.ssl'被導入。檢查你的'PYTHONPATH'環境變量以確保它沒有被設置爲包含超過它的值。 –