2015-04-06 56 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