400客户端错误:错误的url请求:https://api.dropboxapi.com/2/files/list\u fold

2024-04-26 17:26:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图在Dropbox业务帐户中列出团队成员的文件夹。

https://api.dropboxapi.com/2/files/list_folder要求我们添加Dropbox-API-Select-User头,但它似乎不起作用。

到目前为止这是我的代码:

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())

注意,post()函数中的json=参数将内容类型设置为application/json,因此这应该是正确的。

上面的代码引发了一个异常:

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

我尝试使用团队成员ID(bdmid:)而不是帐户ID,但得到了相同的错误。

你知道怎么了吗?

提前谢谢你的帮助。

如果这有什么不同的话,我将使用Python3.6。


Tags: httpscomapiidjsonurl帐户files
1条回答
网友
1楼 · 发布于 2024-04-26 17:26:24

首先,我应该注意到我们确实建议使用官方的Dropbox API v2 Python SDK,因为它为您处理了很多底层的网络/格式工作。也就是说,如果您愿意,您当然可以直接这样使用底层HTTPS endpoints

无论如何,在处理此类问题时,请确保打印出响应本身的主体,因为它将包含一个更有用的错误消息。你可以这样做:

print(r.text)

在这种情况下,此代码将生成一条错误消息:

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

另一个问题是,对于API v2,根路径应该指定为空字符串,""

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

这是因为当使用这样的member file access功能时,应该提供成员ID,而不是帐户ID

因此,修复这些问题时,工作代码如下所示:

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())

编辑后添加,如果要使用Dropbox API v2 Python SDK进行添加,可以使用^{}如下:

import dropbox

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

print(dbx_user.files_list_folder(""))

相关问题 更多 >