I’m working on a project to backup all my Google Drive files to my computer using the Drive API. Most files download fine but some are giving me weird errors.
These problem files show up in my file listings and I can get their metadata with no issues. The API shows I own these files with full permissions. But when I try to use the export URLs to download them, I get different error codes.
Error 404 - File Not Found
For documents I get “Sorry, the file you have requested does not exist”. For spreadsheets it says “Spreadsheet not found Error 404”.
Error 401 - Permission Denied
Some files return “Permission denied Error 401” even though I’m the owner.
Error 500 - Server Error
Many files fail with “Conversion failed unexpectedly Error 500”.
What I want to know is:
- Are these files actually lost when I get 404 errors?
- Should I retry later for the 500 errors?
- Why would I get permission denied on my own files?
Here’s example code showing the issue:
def download_file(file_id):
try:
file_metadata = drive_service.files().get(fileId=file_id).execute()
export_url = file_metadata['exportLinks']['application/pdf']
response = requests.get(export_url, headers=headers)
if response.status_code == 200:
return response.content
else:
print(f"Export failed with status {response.status_code}")
return None
except Exception as e:
print(f"Error downloading file: {e}")
return None
The file shows as owned by me but export always fails. Any ideas what’s causing this?
same thing happened to me. those 500 errors usually mean google’s export service is choking on large files or weird formatting - definitely retry with delays between attempts. for the 401 errors, check your access token scopes. sometimes drive.readonly isn’t enough and you’ll need drive.file or full drive access, even for your own files. the 404s are probably hitting files that can’t actually export to pdf even though they show up in exportLinks.
I’ve hit this same export problem before. The issue is you’re calling exportLinks directly with requests, which messes up authentication since those links need proper OAuth tokens in the headers. Ditch the manual export URL approach. Use drive_service.files().export(fileId=file_id, mimeType='application/pdf').execute() instead. This keeps authentication working properly. For those 500 errors - that’s just Google’s servers having conversion hiccups. I’ve had good luck with exponential backoff retry logic here. The 401 errors happen because your tokens expired or the headers got mangled in the direct HTTP requests. Don’t worry about the 404 errors - your files aren’t gone. This just means you’re trying to export a file type that doesn’t support the MIME conversion you want. Check the exportLinks dictionary keys first to make sure the format you’re asking for actually exists for that file type.
Your code’s mixing API methods wrong. You’re fetching metadata through the Drive service, then making raw HTTP requests to export URLs - that kills your authentication context. The Drive API handles token refresh and headers automatically, but your manual requests don’t get any of that. Those 401 errors? Your export URLs need specific auth that gets stripped when you use requests directly. The 500 errors happen all the time with collaborative docs that have complex formatting or embedded stuff. The conversion engine chokes on certain elements and crashes. I always use a fallback - try PDF export first, then fall back to the original format if it’s available. Don’t panic about 404 errors - your files aren’t lost. It just means that specific export format isn’t available for that document type, even though it shows up in exportLinks.