0

我正在使用 Request 库来测试 ReST API。我在尝试将以下 cURL 转换为请求库调用时遇到问题。

curl https://upload.box.com/api/2.0/files/content \ -H "Authorization: Bearer ACCESS_TOKEN" \ -F filename=@FILE_NAME \ -F parent_id=PARENT_FOLDER_ID

我尝试了这个论坛中的许多建议。但没有任何效果。

我在评论后添加的代码是:

我写的代码是:

def upload_a_file(url, folder_id, file_name, access_token): field_values = "{\'filename\': (filename, open("+file_name+", \'rb\'))}" payload = "{\'parent_id\':"+folder_id+"}" request_headers = {'Authorization': 'Bearer'+access_token} result = requests.post(url, headers=request_headers, data=payload, files=field_values) response = result.json() print response

4

2 回答 2

1

我假设您的意思是请求库?

如果是这样,这就是我的做法。

access_token = <user access token>
filename = <name of the file as you want it to appear on Box>
src_file = the actual file path
parent_id = the id of the folder you want to upload to

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=data, files=files, headers=headers)
file_info = response.json()
于 2014-09-20T16:47:32.060 回答
0

我按照http://www.snip2code.com/Snippet/67408/Show-progress-bar-when-uploading-a-file中给出的示例进行操作。

我能够成功进行 API 调用。

于 2014-09-21T05:23:15.053 回答