2015-04-06 53 views
0

我是新來的編程和學習巨蟒,所以請多多包涵,我感謝幫助....經與Box.com API麻煩上傳文件

我工作的一個項目,我需要上傳文件存儲服務和我目前正在嘗試使用框API。我想這個網頁上的代碼的工作:

how to use python's Request library to make an API call with an attachment and a parameter

import requests 
import json 

#the user access token 
access_token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' 
#the name of the file as you want it to appear in box 
filename = 'box_file' 
#the actual file path 
src_file = "C:\Python\Wildlife.wmv" 
#the id of the folder you want to upload to 
parent_id = '0' 

headers = { 'Authorization: Bearer {0}'.format(access_token)} 
url = 'https://upload.box.com/api/2.0/files/content' 
files = { 'filename': (filename, open(src_file,'rb')) } 
data = { "parent_id": parent_id } 
response = requests.post(url, data, files, headers) 
file_info = response.json() 

我已經嘗試了一些真的還沒有得到我任何接近不同的東西,所以我張貼我的輕微調整他們的代碼。目前,我收到此錯誤:

Traceback (most recent call last): 
    File "transfer2.py", line 18, in <module> 
    response = requests.post(url, data, files, headers) 
TypeError: post() takes from 1 to 3 positional arguments but 4 were given 

我也有過與文檔信息= response.json問題()」在我的一些其他實驗如果有人可以幫助我得到這個工作,我將不勝感激。

我使用Python 3,是否可以幫助

編輯4/6 按照要求,我改變了這一行: 響應= requests.post(URL,數據=數據,文件=文件,標題=標題)

這是錯誤我現在得到:

Traceback (most recent call last): 
    File "transfer2.py", line 18, in <module> 
    response = requests.post(url, data=data, files=files, headers=headers) 
    File "C:\Python34\lib\site-packages\requests\api.py", line 108, in post 
    return request('post', url, data=data, json=json, **kwargs) 
    File "C:\Python34\lib\site-packages\requests\api.py", line 50, in request 
    response = session.request(method=method, url=url, **kwargs) 
    File "C:\Python34\lib\site-packages\requests\sessions.py", line 450, in request 
    prep = self.prepare_request(req) 
    File "C:\Python34\lib\site-packages\requests\sessions.py", line 381, in prepare_request 
    hooks=merge_hooks(request.hooks, self.hooks), 
    File "C:\Python34\lib\site-packages\requests\models.py", line 305, in prepare 
    self.prepare_headers(headers) 
    File "C:\Python34\lib\site-packages\requests\models.py", line 410, in prepare_headers 
    self.headers = CaseInsensitiveDict((to_native_string(name), value) for name, value in headers.items()) 
AttributeError: 'set' object has no attribute 'items' 
+1

還有一個新的[Box Python SDK](https://github.com/box/box-python-sdk),這可能會使使用API​​變得更容易一些。有一個如何上傳文件的例子[這裏](https://github.com/box/box-python-sdk/blob/master/demo/example.py)。 – Greg 2015-04-06 00:56:44

+0

Greg,是否可以使用SDK上傳指定目錄中的所有文件? – 2015-04-12 20:01:22

+0

我不認爲有。您需要遍歷文件夾中的文件並分別上傳每個文件。如果您還有其他關於Python SDK的問題,我建議發佈一個單獨的問題。 – Greg 2015-04-12 20:45:02

回答

2

在請求庫request.post(),頭和文件只有兩個關鍵字參數,我也會使數據的關鍵字參數,如:

response = requests.post(url, data=data, files=files, headers=headers) 
+0

標題需要是一個字典,並且你已經創建了一個'set()',嘗試'headers = {'Authorization':'Bearer {0}'format(access_token)}' – AChampion 2015-04-07 01:13:18

+0

就是這樣。謝謝! – 2015-04-07 01:39:33

1
from boxsdk import Client, OAuth2 
oauth = OAuth2(client_id="dlpjkcxxxxxxxxxxxxxxxxxxxxcom",client_secret="xxxxxxxxxxxxxxxxxxxxxxxxx", access_token="xxxxxxxxxxxxxxxxxxxxxxxxxxx",) 
client = Client(oauth) 
shared_folder = client.folder(folder_id='0',).create_subfolder('sxxxx') 
uploaded_file = shared_folder.upload('/root/xxxxxx.txt') 
+0

歡迎登機!在StackOverflow中,僅有代碼的答案不被視爲良好的樣式。請添加一些關於您的代碼段的解釋和說明。 – quinz 2017-10-05 08:07:41

+0

請添加一些關於您的答案的描述。 – 2017-10-05 08:21:52