Steve-O
Steve-O

Reputation: 397

Having trouble uploading files with Box.com API

I am new to programming and learning python, so please bear with me, I appreciate the help....

I am working on a project where I need to upload files to storage services and I am currently trying to use the box API. I am trying to work with the code on this page:

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()

I have tried a number of different things that really haven't gotten me any closer, so I am posting my slight adaptation of their code. Currently I am getting this error:

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

I have also had issues with the file_info = response.json()" in some of my other experiments. If someone could help me to get this working I would greatly appreciate it.

I am using python 3 if that helps.

edit 4/6 As requested, I changed this line: response = requests.post(url, data=data, files=files, headers=headers)

This is the error I now get:

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'

Upvotes: 0

Views: 2167

Answers (2)

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')

Upvotes: 1

AChampion
AChampion

Reputation: 30288

In the requests library for request.post(), headers and files are both keyword arguments only, I would also make data a keyword argument, e.g.:

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

Upvotes: 2

Related Questions