-2

How can I upload files to Dropbox in python using access tokens?

I have tried with the following code for uploading the file

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)

I got an error in response like this:

Error in call to API function "files/upload": Must provide HTTP header "Dropbox-API-Arg" or URL parameter "arg".

How can I modify the code for that?

kkr
  • 115
  • 1
  • 14
  • Possible duplicate of [upload file to my dropbox from python script](https://stackoverflow.com/questions/23894221/upload-file-to-my-dropbox-from-python-script) – cullzie Feb 12 '19 at 06:48

1 Answers1

0

Add the following header and read the docs to update your request with the necessary headers:

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"
cullzie
  • 2,705
  • 2
  • 16
  • 21
  • headers["Dropbox-API-Arg"] = {\"path\": \"/folder/file.txt\", \"mode\": \"add\", \"autorename\": true, \"mute\": false,\"strict_conflict\": false}" SyntaxError: unexpected character after line continuation character @cullzie I got this as error – kkr Feb 12 '19 at 06:06
  • After that also same error, `headers["Dropbox-API-Arg"] = {\"path\": \"folder/file.txt\", \"mode\": \"add\", \"autorename\": true, \"mute\": false, \"strict_conflict\": false}` SyntaxError: unexpected character after line continuation character – kkr Feb 12 '19 at 06:11
  • Updated to remove the backslashes – cullzie Feb 12 '19 at 06:15
  • I got this error, 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 – kkr Feb 12 '19 at 06:18