I’m trying to delete a file from Google Drive using the Google Drive API and Python’s requests library, but I’m encountering a 403 Forbidden error.
Here’s my code:
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())
Error Message:
Exception: 403 Client Error: Forbidden for URL: https://www.googleapis.com/drive/v3/files/12345?supportAllDrives=True
OAuth Scopes Configured:
File Properties:
{
"kind": "drive#fileList",
"incompleteSearch": "False",
"files": [
{
"kind": "drive#file",
"fileExtension": "pdf",
"capabilities": {
"canDelete": "False",
"canChangeViewersCanCopyContent": "False",
"canRemoveChildren": "False",
"canRemoveContentRestriction": "False",
"canDeleteChildren": "False"
}
}
]
}
From the file properties, it appears that canDelete is set to False.
Question:
Why am I receiving a 403 Forbidden error when trying to delete this file, and how can I resolve it so that I can successfully delete the file using the Google Drive API?
Any insights or suggestions would be greatly appreciated!
Update 1:
@Linda,
Thanks for your suggestion. I tried using files.get() and received a valid JSON response:
{
"kind": "drive#file",
"id": "12345",
"name": "sample_file.pdf",
"teamDriveId": "TEST"
}
I’m uploading files using this endpoint with my OAuth 2.0 client, following the Google Drive API documentation on managing uploads:
https://www.googleapis.com/upload/drive/v3/files
I’m a bit confused because my OAuth 2.0 client has the following scopes:
Non-Sensitive Scopes:
• .../auth/drive.file — See, edit, create, and delete only the specific Google Drive files that you use with this app.
• .../auth/drive.appdata — See, create, and delete its own configuration data in your Google Drive.
Sensitive Scopes:
• .../auth/docs — See, edit, create, and delete all of your Google Drive files.
Given these scopes and the fact that I can retrieve the file metadata, I would expect to be able to delete the file. Do you have any insights into why I’m encountering a 403 Forbidden error when attempting to delete it?
Let me know if there’s anything else you’d like to add or adjust!
403 Client Error: Forbidden
Normally means either the file doesnt exist or you dont have access.
Do a files.list or file.get and make sure you can see the file
If you can see it but still cant delete it check the permissions you may not be the owner.
Remember the user you got the access token from denotes access level.