Python调用Dropbox API获取本地Dropbox路径遇根目录不支持错误
问题描述
需要在任意Mac/Windows机器上获取本地Dropbox文件夹路径(无论该文件夹位于副硬盘还是自定义命名,例如E:\My_funny_dropbox)。已配置拥有files.metadata.read权限的Dropbox API令牌(可正常读取环境变量中的令牌),但调用dbx.files_get_metadata("/")时触发错误,提示path: The root folder is unsupported,传入空字符串也无法解决。完整报错日志如下:
E:\Dropbox\pythonProject1\Scripts\python.exe "E:\Dropbox\pythonProject1\dropbox_folder.py" The Dropbox access token is: *** got the right token *** Traceback (most recent call last): File "E:\Dropbox\pythonProject1\Lib\site-packages\requests\models.py", line 971, in json return complexjson.loads(self.text, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\me\AppData\Local\Programs\Python\Python311\Lib\json\__init__.py", line 346, in loads return _default_decoder.decode(s) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\me\AppData\Local\Programs\Python\Python311\Lib\json\decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\me\AppData\Local\Programs\Python\Python311\Lib\json\decoder.py", line 355, in raw_decode raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "E:\Dropbox\pythonProject1\Lib\site-packages\dropbox\dropbox_client.py", line 624, in raise_dropbox_error_for_resp if res.json()['error'] == 'invalid_grant': ^^^^^^^^^^ File "E:\Dropbox\pythonProject1\Lib\site-packages\requests\models.py", line 975, in json raise RequestsJSONDecodeError(e.msg, e.doc, e.pos) requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "E:\Dropbox\pythonProject1\dropbox_folder.py", line 20, in <module> root_folder = dbx.files_get_metadata("/") ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "E:\Dropbox\pythonProject1\Lib\site-packages\dropbox\base.py", line 1671, in files_get_metadata r = self.request( ^^^^^^^^^^^^^ File "E:\Dropbox\pythonProject1\Lib\site-packages\dropbox\dropbox_client.py", line 326, in request res = self.request_json_string_with_retry(host, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "E:\Dropbox\pythonProject1\Lib\site-packages\dropbox\dropbox_client.py", line 476, in request_json_string_with_retry return self.request_json_string(host, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "E:\Dropbox\pythonProject1\Lib\site-packages\dropbox\dropbox_client.py", line 596, in request_json_string self.raise_dropbox_error_for_resp(r) File "E:\Dropbox\pythonProject1\Lib\site-packages\dropbox\dropbox_client.py", line 632, in raise_dropbox_error_for_resp raise BadInputError(request_id, res.text) dropbox.exceptions.BadInputError: BadInputError('db812a8d6e8948308f2439a3ca50bc53', 'Error in call to API function "files/get_metadata": request body: path: The root folder is unsupported.') Process finished with exit code 1
解决方案
核心说明
Dropbox API的files_get_metadata接口用于获取云端Dropbox文件/文件夹的元数据,无法直接获取本地同步文件夹的路径。要获取本地路径,需读取Dropbox本地客户端生成的配置文件。
Windows系统实现
- 配置文件路径:
%APPDATA%\Dropbox\info.json - 解析逻辑:读取JSON文件,
personal(个人账号)或business(企业账号)节点下的path字段即为本地Dropbox文件夹路径
import json import os def get_local_dropbox_path(): info_path = os.path.expandvars(r"%APPDATA%\Dropbox\info.json") if not os.path.exists(info_path): return None with open(info_path, 'r') as f: data = json.load(f) # 优先返回个人账号路径,无则返回企业账号 return data['personal']['path'] if 'personal' in data else data['business']['path'] if 'business' in data else None
macOS系统实现
- 配置文件路径:
~/Library/Application Support/Dropbox/info.json - 解析逻辑与Windows一致:
import json import os def get_local_dropbox_path(): info_path = os.path.expanduser("~/Library/Application Support/Dropbox/info.json") if not os.path.exists(info_path): return None with open(info_path, 'r') as f: data = json.load(f) return data['personal']['path'] if 'personal' in data else data['business']['path'] if 'business' in data else None
跨平台通用代码
自动识别操作系统,兼容Windows/macOS/Linux:
import json import os import sys def get_local_dropbox_path(): # 根据系统选择配置文件路径 if sys.platform.startswith('win'): info_path = os.path.expandvars(r"%APPDATA%\Dropbox\info.json") elif sys.platform.startswith('darwin'): info_path = os.path.expanduser("~/Library/Application Support/Dropbox/info.json") else: # Linux info_path = os.path.expanduser("~/.dropbox/info.json") if not os.path.exists(info_path): return "未检测到Dropbox本地配置文件" with open(info_path, 'r') as f: data = json.load(f) # 优先返回个人账号路径 if 'personal' in data: return data['personal']['path'] elif 'business' in data: return data['business']['path'] else: return "未找到有效的Dropbox同步账号配置" # 调用示例 print(get_local_dropbox_path())
内容的提问来源于stack exchange,提问作者quickbug
相关产品推荐
相关产品推荐

