如何使用访问令牌将文件上传到python中的Dropbox?
我尝试使用以下代码上传文件
import requests
import json
print("uploading")
token ='#######'
para = {"path": "folder/file.txt", "mode": "add", "autorename": "true", "mute": "false", "strict_conflict": "false"}
headers = {'Authorization': 'Bearer ' + token}
files = {'data': ('metadata', json.dumps(para), 'application/json; charset=UTF-8'), 'file': open("file.txt", "rb")}
response = requests.post("https://content.dropboxapi.com/2/files/upload", headers=headers, files=files)
我在回应中遇到这样的错误:
调用API函数“文件/上传”时出错:必须提供HTTP标头“ Dropbox-API-Arg”或URL参数“ arg”。
我该如何修改代码?
答案 0 :(得分:0)
添加以下标头并阅读docs,以使用必要的标头更新您的请求:
import json
headers["Dropbox-API-Arg"] = json.dumps({"path": "folder/file.txt", "mode": "add", "autorename": true, "mute": false, "strict_conflict": false})
headers["Content-Type"] = "application/octet-stream"