Dropbox API v2-尝试使用files_upload()上传文件-引发TypeError [英] Dropbox API v2 - trying to upload file with files_upload() - throws TypeError
问题描述
我一直在尝试使用python3中的files_upload()函数将一个简单的文件上传到保管箱
I have been trying to upload a simple file to dropbox using the files_upload() function in python3
即使尝试了教程我收到了一个错误,但我不明白为什么。我在这里想念什么?
Even trying out the code in the tutorial provided on Dropbox's site I get an error and I don't understand why. What am I missing here?
这是我的代码:
import dropbox
dbx = dropbox.Dropbox("my_access_token")
data = "asd"
dbx.files_upload(data, '/file.txt')
这是错误消息当我尝试运行它时得到:
Traceback (most recent call last):
File "dbox.py", line 7, in <module>
dbx.files_upload(data, '/file.txt')
File "/usr/local/lib/python3.4/dist-packages/dropbox/base.py", line 1225, in files_upload
f,
File "/usr/local/lib/python3.4/dist-packages/dropbox/dropbox.py", line 249, in request
timeout=timeout)
File "/usr/local/lib/python3.4/dist-packages/dropbox/dropbox.py", line 341, in request_json_string_with_retry
timeout=timeout)
File "/usr/local/lib/python3.4/dist-packages/dropbox/dropbox.py", line 385, in request_json_string
type(request_binary))
TypeError: expected request_binary as binary type, got <class 'str'>
我尝试了不同的方法:
1。
with open("/home/pi/Desktop/dbox/asd.txt", "rb") as f:
dbx.files_upload(f, '/asd.txt', mute = True)
2。
dbx.files_upload("hello", "")
3。
dbx.files_upload("hello", "/")
,但每次都会收到相同的错误。
but I get the same error every time.
推荐答案
来自<一个href = http://dropbox-sdk-python.readthedocs.io/en/master/moduledoc.html#dropbox.dropbox.Dropbox.files_upload rel = noreferrer>此文档,它似乎 files_upload()
的第一个参数需要为字节对象。这意味着您已经接近:
From this documentation, it appears that the first argument to files_upload()
needs to be a bytes object. Which means you got close with:
with open("/home/pi/Desktop/dbox/asd.txt", "rb") as f:
dbx.files_upload(f, '/asd.txt', mute = True)
请尝试执行此操作( f.read()
返回 bytes
个对象):
Try this instead (f.read()
returns a bytes
object):
with open("/home/pi/Desktop/dbox/asd.txt", "rb") as f:
dbx.files_upload(f.read(), '/asd.txt', mute = True)
您也可以尝试传递 data.encode(whatever_encoding)
而不是仅仅传递 data
。我不确定为什么在您链接的教程中没有提到这一点。
You could also try passing data.encode(whatever_encoding)
instead of just data
. I am not sure why this is not mentioned in the tutorial that you linked.
这篇关于Dropbox API v2-尝试使用files_upload()上传文件-引发TypeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!