PyDrive复制文件报错:已认证用户无法复制该文件 求助
Hey there, let's tackle that frustrating "This file cannot be copied by the authenticated user" error you're hitting with Google Drive API. Let's break down the problem and fix it step by step.
The Error You're Seeing
googleapiclient.errors.HttpError: https://www.googleapis.com/drive/v2/files/1yH7LZ54uaiOl4T0J5UoJ9zb30cVKtW19/copy?alt=json 返回"This file cannot be copied by the authenticated user."
Why This Happens
Most of the time, this error boils down to file permission restrictions—not issues with your code (though we'll double-check that too):
- Read-only access: The file you're trying to copy is shared with you in "view-only" mode; you need edit permissions to make a copy.
- Owner-imposed restrictions: The file's owner might have enabled the "Prevent viewers from copying, printing, and downloading" setting (common for sensitive Google Docs/Sheets/Slides).
- OAuth account mismatch: Even if your scopes are correct, you might be authenticating with a Google account that doesn't have access to the target file.
Fixes to Try
1. Verify File Permissions Manually
First, log into your Google Drive account and locate the file with ID 1yH7LZ54uaiOl4T0J5UoJ9zb30cVKtW19:
- Right-click the file → Share → Check your access level (it should be "Editor", not "Viewer" or "Commenter").
- If you're an editor, click the gear icon in the share menu and confirm there's no restriction blocking copy permissions (like "Prevent editors from changing access"—though this is less common than viewer-level restrictions).
2. Adjust Your Code (Optional: Upgrade to Drive API v3)
You're using Drive API v2, which is still functional, but v3 is the current recommended version and has simpler syntax. Here's how to update your code:
First, modify your DriveBox class to use v3:
class DriveBox : g_drive = None instance = None def __new__(cls, *args, **kwargs): if cls.instance is None: gauth = GoogleAuth() try: gauth.CommandLineAuth() except (AuthenticationError, RefreshError) as e: print(e) # Add api_version='v3' to use the latest API version cls.g_drive = GoogleDrive(gauth, api_version='v3') cls.instance = super(DriveBox, cls).__new__(cls) return cls.instance
Then update the copy logic (v3 uses name instead of title, and parents are simplified):
for file in list_files: drive.g_drive.auth.service.files().copy( fileId=file['id'], body={"parents": [new_folder['id']], 'name': file['name']} ).execute()
If you want to stick with v2, your existing parent format is correct—so the issue is almost certainly permission-related.
3. Confirm OAuth Authentication
Double-check that the account you're authenticating with via CommandLineAuth() is the same one that has access to the target file. It's easy to accidentally log into a secondary Google account without noticing, which would block access.
Extra Checks
- If the file lives in a shared folder, ensure the folder itself doesn't have restrictions that block copying files within it.
- For Google Workspace accounts, your admin might have domain-wide rules limiting file copying—reach out to them if you suspect this is the case.
内容的提问来源于stack exchange,提问作者r1299597

