【问题标题】:Uploading files to dropbox using python with the access tokens使用带有访问令牌的 python 将文件上传到 Dropbox
【发布时间】:2019-07-05 16:59:12
【问题描述】:

如何使用访问令牌在 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 标头“Dropbox-API-Arg”或 URL 参数“arg”。

如何修改代码?

【问题讨论】:

标签: python upload dropbox


【解决方案1】:

添加以下标头并阅读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"

【讨论】:

  • headers["Dropbox-API-Arg"] = {\"path\": \"/folder/file.txt\", \"mode\": \"add\", \ "autorename\": true, \"mute\": false,\"strict_conflict\": false}" SyntaxError: 行继续符@cullzie 后出现意外字符我得到这个错误
  • 之后也是同样的错误,headers["Dropbox-API-Arg"] = {\"path\": \"folder/file.txt\", \"mode\": \"add\", \"autorename\": true, \"mute\": false, \"strict_conflict\": false} SyntaxError: unexpected character after line continuation character
  • 更新删除了反斜杠
  • 我收到了这个错误,requests.exceptions InvalidHeader: Value for header {Dropbox-API-Arg: {'path': 'folder/file.txt', 'mute': 'false', 'mode': 'add', 'strict_conflict': 'false', 'autorename': 'true'}} must be of type str or bytes, not
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-01
相关资源
最近更新 更多