我尝试使用 Google Drive API 和 Python 的请求库从 Google Drive 中删除文件,但遇到 403 禁止错误。
这是我的代码:
import requests
headers = {
"Authorization": GCP_ACCESS_TOKEN,
"Content-Type": "application/json"
}
file_id = "12345"
params = {
"supportsAllDrives": True
}
response = requests.delete(
f"https://www.googleapis.com/drive/v3/files/{file_id}",
headers=headers,
params=params
)
print(response.json())
错误信息:
Exception: 403 Client Error: Forbidden for URL: https://www.googleapis.com/drive/v3/files/12345?supportAllDrives=True
配置的 OAuth 范围:
文件属性:
{
"kind": "drive#fileList",
"incompleteSearch": "False",
"files": [
{
"kind": "drive#file",
"fileExtension": "pdf",
"capabilities": {
"canDelete": "False",
"canChangeViewersCanCopyContent": "False",
"canRemoveChildren": "False",
"canRemoveContentRestriction": "False",
"canDeleteChildren": "False"
}
}
]
}
从文件属性来看,canDelete 似乎设置为 False。
问题:
为什么我在尝试删除此文件时收到 403 Forbidden 错误?如何解决该错误,以便我可以使用 Google Drive API 成功删除该文件?
任何见解或建议将不胜感激!
403 客户端错误:禁止
通常意味着该文件不存在或您无权访问。
执行 files.list 或 file.get 并确保您可以看到该文件
如果您可以看到它但仍然无法删除它,请检查权限,您可能不是所有者。
请记住您获得访问令牌的用户表示访问级别。