Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

400 Client Error: Bad Request for url: https://api.dropboxapi.com/2/files/list_folder

I'm trying to list the folders for a team member on our Dropbox Business account.

https://api.dropboxapi.com/2/files/list_folder requires that we add the Dropbox-API-Select-User header, but it does not seem to be working.

This is my code so far:

import requests

url = "https://api.dropboxapi.com/2/files/list_folder"

headers = {
    "Authorization": "Bearer MY_TOKEN",
    "Dropbox-API-Select-User": "dbid:ACCOUNT_ID"
    }

data = {
    "path": "/",
}

r = requests.post(url, headers=headers, json=data)
r.raise_for_status()
print(r.json())

Note that the json= argument in the post() function sets the content type to application/json so that should be correct.

The code above raises an exception:

requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://api.dropboxapi.com/2/files/list_folder

I have tried using the team member ID (bdmid:) instead of the account ID but got the same error.

Do you have any idea what's wrong?

Thanks in advance for any help.

I'm using Python 3.6 if that makes any difference.

like image 290
blokeley Avatar asked Mar 09 '23 14:03

blokeley


1 Answers

First, I should note that we do recommend using the official Dropbox API v2 Python SDK as it takes care of a lot of the underlying networking/formatting work for you. That said, you can certainly use the underlying HTTPS endpoints directly like this if you prefer.

Anyway, when dealing with issues like this, be sure to print out the body of the response itself, as it will contain a more helpful error message. You can do so like this:

print(r.text)

In this case with this code, that yields an error message:

Error in call to API function "files/list_folder": Invalid select user id format

Another issue is that with API v2, the root path is supposed to be specified as empty string, "":

Error in call to API function "files/list_folder": request body: path: Specify the root folder as an empty string rather than as "/".

That's because when using the member file access feature like this, you are supposed to supply the member ID, not the account ID.

So, fixing those issues, the working code looks this:

import requests

url = "https://api.dropboxapi.com/2/files/list_folder"

headers = {
    "Authorization": "Bearer MY_TOKEN",
    "Dropbox-API-Select-User": "dbmid:MEMBER_ID"
    }

data = {
    "path": "",
}

r = requests.post(url, headers=headers, json=data)
print(r.text)
r.raise_for_status()
print(r.json())

Edited to add, if you want to use the Dropbox API v2 Python SDK for this, you would use DropboxTeam.as_user like this:

import dropbox

dbx_team = dropbox.DropboxTeam("MY_TOKEN")
dbx_user = dbx_team.as_user("dbmid:MEMBER_ID")

print(dbx_user.files_list_folder(""))
like image 56
Greg Avatar answered Apr 28 '23 04:04

Greg