2012-05-10 48 views
0

我下面的教程here蟒蛇的Dropbox API錯誤

到目前爲止好,但上載例如給我的錯誤。代碼:

from dropbox import client, rest, session 

f = open('txt2.txt') # upload a file 
response = client.put_file('/magnum-opus.txt', f) 
print "uploaded:", response 

錯誤:

Traceback (most recent call last): 
    File "dropbox_ul.py", line 4, in <module> 
    response = client.put_file('/magnum-opus.txt', f) 
AttributeError: 'module' object has no attribute 'put_file' 

我有什麼錯?

編輯:我正在嘗試的新代碼。這實際上來自Dropbox開發者網站。正如我前面所說,我沒有經過認證和設置:

# Include the Dropbox SDK libraries 
from dropbox import client, rest, session 

# Get your app key and secret from the Dropbox developer website 
APP_KEY = 'iqxjea6s7ctxv9j' 
APP_SECRET = 'npac0nca3p3ct9f' 

# ACCESS_TYPE should be 'dropbox' or 'app_folder' as configured for your app 
ACCESS_TYPE = 'dropbox' 

sess = session.DropboxSession(APP_KEY,APP_SECRET, ACCESS_TYPE) 

request_token = sess.obtain_request_token() 

# Make the user sign in and authorize this token 
url = sess.build_authorize_url(request_token) 
print "url:", url 
print "Please authorize in the browser. After you're done, press enter." 
raw_input() 

# This will fail if the user didn't visit the above URL and hit 'Allow' 
access_token = sess.obtain_access_token(request_token) 

client = client.DropboxClient(sess) 
print "linked account:", client.account_info() 

f = open('txt2.txt') 
response = client.put_file('/magnum-opus.txt', f) 
print "uploaded:", response 

folder_metadata = client.metadata('/') 
print "metadata:", folder_metadata 

f, metadata = client.get_file_and_metadata('/magnum-opus.txt',rev='362e2029684fe') 
out = open('magnum-opus.txt', 'w') 
out.write(f) 
print(metadata) 

和錯誤:

url: https://www.dropbox.com/1/oauth/authorize?oauth_token=jqbasca63c0a84m 
    Please authorize in the browser. After you're done, press enter. 

    linked account: {'referral_link': 'https://www.dropbox.com/referrals/NTMxMzM4NjY5', 'display_name': 'Greg Lorincz', 'uid': 3133866, 'country': 'GB', 'quota_info': {'shared': 78211, 'quota': 28185722880, 'normal': 468671581}, 'email': '[email protected]'} 
    Traceback (most recent call last): 
     File "dropb.py", line 28, in <module> 
     response = client.put_file('/magnum-opus.txt', f) 
     File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/dropbox-1.4-py2.7.egg/dropbox/client.py", line 149, in put_file 
     return RESTClient.PUT(url, file_obj, headers) 
     File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/dropbox-1.4-py2.7.egg/dropbox/rest.py", line 146, in PUT 
     return cls.request("PUT", url, body=body, headers=headers, raw_response=raw_response) 
     File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/dropbox-1.4-py2.7.egg/dropbox/rest.py", line 113, in request 
     raise ErrorResponse(r) 
    dropbox.rest.ErrorResponse: [403] 'The provided token does not allow this operation' 

回答

2

您還沒有初始化客戶端對象。再參考教程,你會看到這一點:

client = client.DropboxClient(sess) 

的SESS對象還必須調用客戶端模塊的DropboxClient方法之前初始化:

sess = session.DropboxSession(APP_KEY, APP_SECRET, ACCESS_TYPE) 

你應該擁有所有必需的參數(即,APP_KEY,APP_SECRET,ACCESS_TYPE)在您註冊您的應用程序時分配給您。

+0

酷!感謝您的幫助,請嘗試! – alkopop79

+0

試圖添加所有認證密鑰的行,它給了我一個認證錯誤。 – alkopop79

+0

似乎無法將錯誤粘貼到這裏,也沒有新的代碼。 – alkopop79

0

我遵循你的編輯代碼和事情完美解決。

from dropbox import client, rest, session 

# Get your app key and secret from the Dropbox developer website 
app_key = 'enter-your-app_key' 
app_secret = 'enter-your-app_secret' 

ACCESS_TYPE = 'dropbox' 

sess = session.DropboxSession(app_key, app_secret, ACCESS_TYPE) 

request_token = sess.obtain_request_token() 

# Make the user sign in and authorize this token 
url = sess.build_authorize_url(request_token) 
print "url:", url 
print "Please authorize in the browser. After you're done, press enter." 
raw_input() 

# This will fail if the user didn't visit the above URL and hit 'Allow' 
access_token = sess.obtain_access_token(request_token) 

client = client.DropboxClient(sess) 
print "linked account:", client.account_info() 

f = open('/home/anurag/Documents/sayan.odt') 
response = client.put_file('/sayan.odt', f) 
print "uploaded:", response 

注意您的系統上的響應和文件位置,在您的代碼中不匹配。

謝謝。