Google共享文件夹文件获取异常:仅得到“Getting Started”文件求助
Hey there! Let's work through this frustrating issue you're facing—only pulling up the "Getting Started" file when trying to access a shared folder via the Google Drive API is totally fixable, so let's break it down step by step.
1. You're Using the Wrong API Method
It sounds like you tried using files().get, but that method is designed to fetch details of a single specific file—not list all content in a folder. To see every file in your shared folder, you need to use files().list with a filter query targeting the folder's contents.
2. Permissions Are Likely Missing
Double-check that your service account has been granted access to the shared folder:
- Locate your service account's email in your JSON key file (look for the
client_emailfield) - Ask the folder owner to share the folder with this email, granting at least "Viewer" permissions (or "Editor" if you need to download/write files)
3. Clean Up Your Authentication Setup
You've imported two different service account auth libraries (google.oauth2.service_account and oauth2client.service_account)—this can cause conflicts. Stick to the newer, maintained google-auth library for better reliability.
Corrected Code Example
Here's a cleaned-up script that will list all files in your shared folder and let you download them:
from google.oauth2 import service_account from googleapiclient.discovery import build import io from googleapiclient.http import MediaIoBaseDownload # Configuration - update these values SERVICE_ACCOUNT_KEY_PATH = "path/to/your/service-account-key.json" SHARED_FOLDER_ID = "your-shared-folder-id-here" SCOPES = ["https://www.googleapis.com/auth/drive.readonly"] # Authenticate with the Drive API credentials = service_account.Credentials.from_service_account_file( SERVICE_ACCOUNT_KEY_PATH, scopes=SCOPES ) drive_service = build("drive", "v3", credentials=credentials) # List all non-trashed files in the shared folder def list_shared_folder_contents(folder_id): results = drive_service.files().list( q=f"'{folder_id}' in parents and trashed=false", fields="files(id, name, mimeType)", supportsAllDrives=True # Add this if the folder is in a Shared Drive/Team Drive ).execute() items = results.get("files", []) if not items: print("No files found in the shared folder.") return [] print("Files in shared folder:") for item in items: print(f"- {item['name']} (ID: {item['id']})") return items # Download a specific file from the folder def download_file(file_id, file_name): request = drive_service.files().get_media(fileId=file_id) fh = io.FileIO(file_name, "wb") downloader = MediaIoBaseDownload(fh, request) done = False while done is False: status, done = downloader.next_chunk() print(f"Download progress: {int(status.progress() * 100)}%") print(f"File saved as {file_name}") # Run the functions folder_files = list_shared_folder_contents(SHARED_FOLDER_ID) # Example: Download the first file in the list if folder_files: download_file(folder_files[0]["id"], folder_files[0]["name"])
Key Notes:
- The
qparameter filters files where the shared folder is a parent, and excludes any trashed files - Add
supportsAllDrives=Trueif your shared folder is part of a Google Workspace Shared Drive/Team Drive - Double-check your folder ID is correct (you can grab it from the folder's URL in Google Drive)
If you still don't see files after this, triple-check that the service account email was correctly added to the folder's sharing settings—typos here are a super common culprit!
内容的提问来源于stack exchange,提问作者Rod0n

