我是新來的編程和學習巨蟒,所以請多多包涵,我感謝幫助....經與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'
還有一個新的[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
Greg,是否可以使用SDK上傳指定目錄中的所有文件? – 2015-04-12 20:01:22
我不認爲有。您需要遍歷文件夾中的文件並分別上傳每個文件。如果您還有其他關於Python SDK的問題,我建議發佈一個單獨的問題。 – Greg 2015-04-12 20:45:02