使用 python 和访问令牌将文件上传到保管箱

Uploading files to dropbox using python with the access tokens

如何使用访问令牌将文件上传到 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 函数 "files/upload" 时出错:必须提供 HTTP header "Dropbox-API-Arg" 或 URL 参数 "arg".

我该如何修改代码?

添加以下 header 并阅读 docs 以使用必要的 header 更新您的请求:

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"