I’m having trouble getting direct image URLs from Google Drive files. My script was working fine until recently but now some images trigger downloads instead of displaying directly.
I wrote a Python script that fetches image files from a shared Google Drive folder and creates direct links for embedding. The script uses the Google Drive API to get file IDs and then constructs direct URLs.
Here’s my current implementation:
api_token = "my_google_api_key_here"
import re
import requests
def fetch_direct_image_urls(drive_folder_url):
# Get the folder ID from URL
folder_id = re.findall(r"/folders/([^\s/]+)", drive_folder_url)[0]
# API call to fetch image files
request_url = f"https://www.googleapis.com/drive/v3/files?q='{folder_id}'+in+parents+and+mimeType+contains+'image/'&key={api_token}"
api_response = requests.get(request_url).json()
direct_urls = []
# Process each image file
for item in api_response['files']:
direct_url = f"https://drive.google.com/uc?id={item['id']}"
print(direct_url)
direct_urls.append(f'<a href="{direct_url}"> <img src="{direct_url}" /> </a>')
return ''.join(direct_urls)
# Test with folder
folder_url = "https://drive.google.com/drive/u/0/folders/**********"
result = fetch_direct_image_urls(folder_url)
Some files work perfectly and display as images, but others force a download instead of showing the actual image. I tried adding export=view parameter but it didn’t help.
Is there a better way to get direct image links that work consistently? Also wondering if there’s a method to extract the direct URL from the file view page itself.
Google Drive direct links have been flaky lately because of their security updates. I switched to the export parameter and it worked for me. Skip the usual uc?id= endpoint and try https://drive.google.com/uc?export=view&id={file_id} for viewing or https://drive.google.com/uc?export=download&id={file_id} then redirect to view. Here’s the thing - Google now treats some image files as documents instead of media, especially bigger files or PNGs with metadata. Files over 25MB almost always force downloads no matter what URL you use. I’d add a file size check in your API and handle large files separately. Also double-check that all files have public sharing turned on - sometimes individual files get different permissions even in the same shared folder.
Same thing happened to me last month - super frustrating. Google’s virus scanner flags certain images and forces them to download instead of display. Add &confirm=t to your URLs: https://drive.google.com/uc?id={item['id']}&confirm=t. WebP files seem to cause more issues than JPGs. I’d convert problem formats on the server side just in case.
Had this exact problem six months ago with a client. Google changed how they handle direct links for certain file types and sizes. I switched to the thumbnail parameter with high resolution instead of the basic uc endpoint. Try this URL structure: https://drive.google.com/thumbnail?id={item['id']}&sz=w2000-h2000. This forces Google to serve the image as a thumbnail and skips the download behavior. Adjust the size parameter as needed. Also check file permissions through the API before building URLs. Files that trigger downloads usually have stricter sharing settings even in the same folder. I started calling the permissions endpoint first - it shows which files will cause problems before you try displaying them.