google-drive
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGoogle Drive
Google Drive
Interact with Google Drive for file management, search, and sharing.
用于与Google Drive交互,实现文件管理、搜索和共享操作。
Installation
安装步骤
-
Install Python dependencies:bash
pip install --user google-auth google-auth-oauthlib google-api-python-client keyring pyyaml -
Download the skill from Releases or use directly from this repository.
-
安装Python依赖包:bash
pip install --user google-auth google-auth-oauthlib google-api-python-client keyring pyyaml -
下载技能包:从Releases下载,或直接使用本仓库代码。
Setup Verification
配置验证
After installation, verify the skill is properly configured:
bash
python scripts/google-drive.py checkThis will check:
- Python dependencies (google-auth, google-auth-oauthlib, google-api-python-client, keyring, pyyaml)
- Authentication configuration
- Connectivity to Google Drive API
If anything is missing, the check command will provide setup instructions.
安装完成后,验证技能是否配置正确:
bash
python scripts/google-drive.py check该命令会检查以下内容:
- Python依赖包(google-auth、google-auth-oauthlib、google-api-python-client、keyring、pyyaml)
- 身份认证配置
- 与Google Drive API的连通性
若有缺失,检查命令会提供对应的配置指导。
Authentication
身份认证
Google Drive uses OAuth 2.0 for authentication. For complete setup instructions, see:
- GCP Project Setup Guide - Create project, enable Drive API
- Google OAuth Setup Guide - Configure credentials
Google Drive采用OAuth 2.0进行身份认证。完整配置说明请参考:
- GCP项目配置指南 - 创建项目、启用Drive API
- Google OAuth配置指南 - 配置凭证
Quick Start
快速开始
-
Create:
~/.config/agent-skills/google.yamlyamloauth_client: client_id: your-client-id.apps.googleusercontent.com client_secret: your-client-secret -
Runto trigger OAuth flow and verify setup.
python scripts/google-drive.py check
-
创建文件:
~/.config/agent-skills/google.yamlyamloauth_client: client_id: your-client-id.apps.googleusercontent.com client_secret: your-client-secret -
运行触发OAuth流程并验证配置。
python scripts/google-drive.py check
OAuth Scopes
OAuth权限范围
The skill requests granular scopes for different operations:
| Scope | Permission | Used For |
|---|---|---|
| Read files and metadata | list, search, download |
| Create/edit files created by app | upload, create folders, share |
| View file metadata only | get file info |
本技能针对不同操作请求细分的权限范围:
| 权限范围 | 权限说明 | 适用场景 |
|---|---|---|
| 读取文件及元数据 | 列出、搜索、下载文件 |
| 创建/编辑本应用生成的文件 | 上传文件、创建文件夹、共享文件 |
| 仅查看文件元数据 | 获取文件信息 |
Scope Errors
权限范围错误处理
If you encounter "insufficient scope" errors, revoke your token and re-authenticate:
- Revoke at https://myaccount.google.com/permissions
- Clear token:
keyring del agent-skills google-drive-token-json - Re-run:
python scripts/google-drive.py check
若遇到“权限范围不足”错误,请撤销现有令牌并重新认证:
- 访问https://myaccount.google.com/permissions撤销权限
- 清除令牌:
keyring del agent-skills google-drive-token-json - 重新运行:
python scripts/google-drive.py check
Commands
命令说明
check
check
Verify configuration and connectivity.
bash
python scripts/google-drive.py checkThis validates:
- Python dependencies are installed
- Authentication is configured
- Can connect to Google Drive API
- Displays your email address and storage usage
验证配置及连通性。
bash
python scripts/google-drive.py check该命令会验证:
- Python依赖包已安装
- 身份认证已配置
- 可连接至Google Drive API
- 显示你的邮箱地址及存储使用情况
auth setup
auth setup
Store OAuth 2.0 client credentials for custom OAuth flow.
bash
python scripts/google-drive.py auth setup \
--client-id YOUR_CLIENT_ID \
--client-secret YOUR_CLIENT_SECRETCredentials are saved to .
~/.config/agent-skills/google-drive.yaml存储OAuth 2.0客户端凭证,用于自定义OAuth流程。
bash
python scripts/google-drive.py auth setup \
--client-id YOUR_CLIENT_ID \
--client-secret YOUR_CLIENT_SECRET凭证将保存至。
~/.config/agent-skills/google-drive.yamlfiles list
files list
List files in your Drive.
bash
undefined列出Drive中的文件。
bash
undefinedList recent files
列出最近文件
python scripts/google-drive.py files list
python scripts/google-drive.py files list
List with search query
带搜索条件列出
python scripts/google-drive.py files list --query "name contains 'report'"
python scripts/google-drive.py files list --query "name contains 'report'"
List with max results
限制结果数量
python scripts/google-drive.py files list --max-results 20
python scripts/google-drive.py files list --max-results 20
List sorted by name
按名称排序
python scripts/google-drive.py files list --order-by "name"
python scripts/google-drive.py files list --order-by "name"
Output as JSON
以JSON格式输出
python scripts/google-drive.py files list --json
**Arguments:**
- `--query`: Drive search query (optional)
- `--max-results`: Maximum number of results (default: 10)
- `--order-by`: Sort order (default: "modifiedTime desc")
- `--json`: Output as JSONpython scripts/google-drive.py files list --json
**参数说明:**
- `--query`: Drive搜索查询语句(可选)
- `--max-results`: 最大返回结果数(默认:10)
- `--order-by`: 排序规则(默认:"modifiedTime desc")
- `--json`: 以JSON格式输出结果files search
files search
Search for files with filters.
bash
undefined带筛选条件搜索文件。
bash
undefinedSearch by name
按文件名搜索
python scripts/google-drive.py files search --name "quarterly report"
python scripts/google-drive.py files search --name "quarterly report"
Search by MIME type
按MIME类型搜索
python scripts/google-drive.py files search --mime-type "application/pdf"
python scripts/google-drive.py files search --mime-type "application/pdf"
Search in a specific folder
在指定文件夹内搜索
python scripts/google-drive.py files search --folder FOLDER_ID
python scripts/google-drive.py files search --folder FOLDER_ID
Combine filters
组合筛选条件
python scripts/google-drive.py files search --name "budget" --mime-type "application/vnd.google-apps.spreadsheet"
python scripts/google-drive.py files search --name "budget" --mime-type "application/vnd.google-apps.spreadsheet"
Output as JSON
以JSON格式输出
python scripts/google-drive.py files search --name "report" --json
**Arguments:**
- `--name`: File name to search for (partial match)
- `--mime-type`: MIME type filter
- `--folder`: Parent folder ID
- `--json`: Output as JSONpython scripts/google-drive.py files search --name "report" --json
**参数说明:**
- `--name`: 要搜索的文件名(支持部分匹配)
- `--mime-type`: MIME类型筛选
- `--folder`: 父文件夹ID
- `--json`: 以JSON格式输出结果files get
files get
Get file metadata by ID.
bash
undefined通过ID获取文件元数据。
bash
undefinedGet file details
获取文件详情
python scripts/google-drive.py files get FILE_ID
python scripts/google-drive.py files get FILE_ID
Output as JSON
以JSON格式输出
python scripts/google-drive.py files get FILE_ID --json
**Arguments:**
- `file_id`: The file ID (required)
- `--json`: Output as JSONpython scripts/google-drive.py files get FILE_ID --json
**参数说明:**
- `file_id`: 文件ID(必填)
- `--json`: 以JSON格式输出结果files download
files download
Download a file from Google Drive.
bash
undefined从Google Drive下载文件。
bash
undefinedDownload a file
下载文件
python scripts/google-drive.py files download FILE_ID --output /path/to/local/file
python scripts/google-drive.py files download FILE_ID --output /path/to/local/file
Short form
简写形式
python scripts/google-drive.py files download FILE_ID -o ./downloaded-file.pdf
**Arguments:**
- `file_id`: The file ID (required)
- `--output`, `-o`: Output file path (required)
**Note:** Google Docs, Sheets, and Slides cannot be downloaded directly. Use the Google Drive web interface to export them.python scripts/google-drive.py files download FILE_ID -o ./downloaded-file.pdf
**参数说明:**
- `file_id`: 文件ID(必填)
- `--output`, `-o`: 本地输出文件路径(必填)
**注意:** Google Docs、Sheets和Slides无法直接下载,请通过Google Drive网页端导出为可下载格式。files upload
files upload
Upload a file to Google Drive.
bash
undefined上传文件至Google Drive。
bash
undefinedUpload a file
上传文件
python scripts/google-drive.py files upload /path/to/file.pdf
python scripts/google-drive.py files upload /path/to/file.pdf
Upload to a specific folder
上传至指定文件夹
python scripts/google-drive.py files upload /path/to/file.pdf --parent FOLDER_ID
python scripts/google-drive.py files upload /path/to/file.pdf --parent FOLDER_ID
Upload with custom name
自定义上传后的文件名
python scripts/google-drive.py files upload /path/to/file.pdf --name "Quarterly Report 2024"
python scripts/google-drive.py files upload /path/to/file.pdf --name "Quarterly Report 2024"
Upload with specific MIME type
指定MIME类型上传
python scripts/google-drive.py files upload /path/to/file --mime-type "text/csv"
python scripts/google-drive.py files upload /path/to/file --mime-type "text/csv"
Output as JSON
以JSON格式输出
python scripts/google-drive.py files upload /path/to/file.pdf --json
**Arguments:**
- `path`: Local file path (required)
- `--parent`: Parent folder ID
- `--mime-type`: MIME type (auto-detected if not provided)
- `--name`: Name for the file in Drive
- `--json`: Output as JSONpython scripts/google-drive.py files upload /path/to/file.pdf --json
**参数说明:**
- `path`: 本地文件路径(必填)
- `--parent`: 父文件夹ID
- `--mime-type`: MIME类型(未指定时自动检测)
- `--name`: 在Drive中显示的文件名
- `--json`: 以JSON格式输出结果folders create
folders create
Create a new folder.
bash
undefined创建新文件夹。
bash
undefinedCreate folder in root
在根目录创建文件夹
python scripts/google-drive.py folders create "New Folder"
python scripts/google-drive.py folders create "New Folder"
Create folder inside another folder
在指定文件夹内创建子文件夹
python scripts/google-drive.py folders create "Subfolder" --parent FOLDER_ID
python scripts/google-drive.py folders create "Subfolder" --parent FOLDER_ID
Output as JSON
以JSON格式输出
python scripts/google-drive.py folders create "Documents" --json
**Arguments:**
- `name`: Folder name (required)
- `--parent`: Parent folder ID
- `--json`: Output as JSONpython scripts/google-drive.py folders create "Documents" --json
**参数说明:**
- `name`: 文件夹名称(必填)
- `--parent`: 父文件夹ID
- `--json`: 以JSON格式输出结果folders list
folders list
List contents of a folder.
bash
undefined列出指定文件夹的内容。
bash
undefinedList folder contents
列出文件夹内容
python scripts/google-drive.py folders list FOLDER_ID
python scripts/google-drive.py folders list FOLDER_ID
List with max results
限制结果数量
python scripts/google-drive.py folders list FOLDER_ID --max-results 50
python scripts/google-drive.py folders list FOLDER_ID --max-results 50
Output as JSON
以JSON格式输出
python scripts/google-drive.py folders list FOLDER_ID --json
**Arguments:**
- `folder_id`: The folder ID (required)
- `--max-results`: Maximum number of results (default: 100)
- `--json`: Output as JSONpython scripts/google-drive.py folders list FOLDER_ID --json
**参数说明:**
- `folder_id`: 文件夹ID(必填)
- `--max-results`: 最大返回结果数(默认:100)
- `--json`: 以JSON格式输出结果share
share
Share a file with a user.
bash
undefined与用户共享文件。
bash
undefinedShare as reader (default)
以读者权限共享(默认)
python scripts/google-drive.py share FILE_ID --email user@example.com
python scripts/google-drive.py share FILE_ID --email user@example.com
Share as writer
以编辑者权限共享
python scripts/google-drive.py share FILE_ID --email user@example.com --role writer
python scripts/google-drive.py share FILE_ID --email user@example.com --role writer
Share as commenter
以评论者权限共享
python scripts/google-drive.py share FILE_ID --email user@example.com --role commenter
python scripts/google-drive.py share FILE_ID --email user@example.com --role commenter
Share without sending notification
共享时不发送通知邮件
python scripts/google-drive.py share FILE_ID --email user@example.com --no-notify
python scripts/google-drive.py share FILE_ID --email user@example.com --no-notify
Output as JSON
以JSON格式输出
python scripts/google-drive.py share FILE_ID --email user@example.com --json
**Arguments:**
- `file_id`: File ID to share (required)
- `--email`: Email address to share with (required)
- `--role`: Permission role - reader, writer, commenter, owner (default: reader)
- `--no-notify`: Don't send notification email
- `--json`: Output as JSONpython scripts/google-drive.py share FILE_ID --email user@example.com --json
**参数说明:**
- `file_id`: 要共享的文件ID(必填)
- `--email`: 共享对象的邮箱地址(必填)
- `--role`: 权限角色 - reader(读者)、writer(编辑者)、commenter(评论者)、owner(所有者)(默认:reader)
- `--no-notify`: 不发送通知邮件
- `--json`: 以JSON格式输出结果permissions list
permissions list
List permissions for a file.
bash
undefined列出文件的权限信息。
bash
undefinedList permissions
列出权限
python scripts/google-drive.py permissions list FILE_ID
python scripts/google-drive.py permissions list FILE_ID
Output as JSON
以JSON格式输出
python scripts/google-drive.py permissions list FILE_ID --json
**Arguments:**
- `file_id`: The file ID (required)
- `--json`: Output as JSONpython scripts/google-drive.py permissions list FILE_ID --json
**参数说明:**
- `file_id`: 文件ID(必填)
- `--json`: 以JSON格式输出结果permissions delete
permissions delete
Remove a permission from a file.
bash
undefined移除文件的某项权限。
bash
undefinedDelete a permission
删除权限
python scripts/google-drive.py permissions delete FILE_ID PERMISSION_ID
**Arguments:**
- `file_id`: The file ID (required)
- `permission_id`: The permission ID to delete (required)python scripts/google-drive.py permissions delete FILE_ID PERMISSION_ID
**参数说明:**
- `file_id`: 文件ID(必填)
- `permission_id`: 要删除的权限ID(必填)Examples
使用示例
Verify Setup
验证配置
bash
python scripts/google-drive.py checkbash
python scripts/google-drive.py checkFind recent PDF files
查找最近的PDF文件
bash
python scripts/google-drive.py files list --query "mimeType='application/pdf'" --max-results 5bash
python scripts/google-drive.py files list --query "mimeType='application/pdf'" --max-results 5Search for documents by name
按名称搜索文档
bash
python scripts/google-drive.py files search --name "project proposal"bash
python scripts/google-drive.py files search --name "project proposal"Download a file
下载文件
bash
undefinedbash
undefinedFirst, find the file ID
先查找文件ID
python scripts/google-drive.py files search --name "report.pdf"
python scripts/google-drive.py files search --name "report.pdf"
Then download it
然后下载
python scripts/google-drive.py files download FILE_ID -o ./report.pdf
undefinedpython scripts/google-drive.py files download FILE_ID -o ./report.pdf
undefinedUpload and share a file
上传并共享文件
bash
undefinedbash
undefinedUpload the file
上传文件
python scripts/google-drive.py files upload ./presentation.pdf --name "Q4 Presentation"
python scripts/google-drive.py files upload ./presentation.pdf --name "Q4 Presentation"
Share with a colleague
与同事共享
python scripts/google-drive.py share FILE_ID --email colleague@example.com --role writer
undefinedpython scripts/google-drive.py share FILE_ID --email colleague@example.com --role writer
undefinedOrganize files into folders
将文件整理至文件夹
bash
undefinedbash
undefinedCreate a folder
创建文件夹
python scripts/google-drive.py folders create "Project Documents"
python scripts/google-drive.py folders create "Project Documents"
Upload files to the folder
上传文件至该文件夹
python scripts/google-drive.py files upload ./doc1.pdf --parent FOLDER_ID
python scripts/google-drive.py files upload ./doc2.pdf --parent FOLDER_ID
python scripts/google-drive.py files upload ./doc1.pdf --parent FOLDER_ID
python scripts/google-drive.py files upload ./doc2.pdf --parent FOLDER_ID
List folder contents
列出文件夹内容
python scripts/google-drive.py folders list FOLDER_ID
undefinedpython scripts/google-drive.py folders list FOLDER_ID
undefinedDrive Search Query Syntax
Drive搜索查询语法
Common search operators:
| Operator | Description | Example |
|---|---|---|
| Name contains string | |
| Exact name match | |
| File type | |
| In folder | |
| Modified after | |
| Trashed status | |
| Starred status | |
| Shared files | |
Combine operators with or :
andorbash
undefined常用搜索操作符:
| 操作符 | 说明 | 示例 |
|---|---|---|
| 文件名包含指定字符串 | |
| 精确匹配文件名 | |
| 文件类型匹配 | |
| 文件位于指定文件夹内 | |
| 文件修改时间晚于指定日期 | |
| 文件是否已放入回收站 | |
| 文件是否已标记星标 | |
| 文件是否是他人共享给自己的 | |
可使用或组合多个操作符:
andorbash
undefinedPDF files modified this year
今年修改过的PDF文件
"mimeType = 'application/pdf' and modifiedTime > '2024-01-01'"
"mimeType = 'application/pdf' and modifiedTime > '2024-01-01'"
Spreadsheets containing 'budget'
包含'budget'的表格文件
"name contains 'budget' and mimeType = 'application/vnd.google-apps.spreadsheet'"
"name contains 'budget' and mimeType = 'application/vnd.google-apps.spreadsheet'"
Files in a specific folder that are not trashed
指定文件夹内未被删除的文件
"'folder_id' in parents and trashed = false"
For the complete reference, see [drive-queries.md](references/drive-queries.md)."'folder_id' in parents and trashed = false"
完整语法参考:[drive-queries.md](references/drive-queries.md)。Common MIME Types
常见MIME类型
| Type | MIME Type |
|---|---|
| Folder | |
| Google Doc | |
| Google Sheet | |
| Google Slides | |
| |
| Word | |
| Excel | |
| PowerPoint | |
| Text | |
| CSV | |
| Image (JPEG) | |
| Image (PNG) | |
| 文件类型 | MIME类型 |
|---|---|
| 文件夹 | |
| Google文档 | |
| Google表格 | |
| Google幻灯片 | |
| |
| Word文档 | |
| Excel表格 | |
| PowerPoint演示文稿 | |
| 文本文件 | |
| CSV文件 | |
| JPEG图片 | |
| PNG图片 | |
Troubleshooting
故障排除
Check command fails
check命令执行失败
Run to diagnose issues. It will provide specific error messages and setup instructions.
python scripts/google-drive.py check运行诊断问题,命令会提供具体错误信息及配置指导。
python scripts/google-drive.py checkAuthentication failed
身份认证失败
- Verify your OAuth client ID and client secret are correct in
~/.config/agent-skills/google.yaml - Token expired or corrupted - clear and re-authenticate:
bash
keyring del agent-skills google-drive-token-json python scripts/google-drive.py check
- 验证中的OAuth客户端ID和密钥是否正确
~/.config/agent-skills/google.yaml - 令牌过期或损坏 - 清除令牌并重新认证:
bash
keyring del agent-skills google-drive-token-json python scripts/google-drive.py check
Permission denied
权限被拒绝
Your OAuth token may not have the necessary scopes. Revoke access at https://myaccount.google.com/permissions, clear your token, and re-authenticate.
你的OAuth令牌可能缺少必要的权限范围。访问https://myaccount.google.com/permissions撤销权限,清除令牌后重新认证。
Cannot download Google Docs
无法下载Google Docs
Google Docs, Sheets, and Slides are not binary files - they cannot be downloaded directly. Use the Google Drive web interface to export them to a downloadable format (PDF, DOCX, etc.).
Google Docs、Sheets和Slides并非二进制文件,无法直接下载。请通过Google Drive网页端将其导出为可下载格式(如PDF、DOCX等)。
Import errors
导入错误
Ensure dependencies are installed:
bash
pip install --user google-auth google-auth-oauthlib google-api-python-client keyring pyyaml确保所有依赖包已安装:
bash
pip install --user google-auth google-auth-oauthlib google-api-python-client keyring pyyamlRate limiting
请求频率限制
Drive API has quota limits. If you hit rate limits, wait a few minutes before retrying. For high-volume usage, consider requesting quota increases in the Google Cloud Console.
Drive API有配额限制。若触发频率限制,请等待几分钟后重试。对于高频使用场景,可在Google Cloud Console申请提高配额。
API Scopes
API权限范围
This skill requests the following OAuth scopes:
- - Read files and metadata
https://www.googleapis.com/auth/drive.readonly - - Create, modify, delete files created by the app
https://www.googleapis.com/auth/drive.file - - View file metadata only
https://www.googleapis.com/auth/drive.metadata.readonly
These scopes provide file management capabilities while following the principle of least privilege.
本技能请求以下OAuth权限范围:
- - 读取文件及元数据
https://www.googleapis.com/auth/drive.readonly - - 创建、修改、删除本应用生成的文件
https://www.googleapis.com/auth/drive.file - - 仅查看文件元数据
https://www.googleapis.com/auth/drive.metadata.readonly
这些权限范围在遵循最小权限原则的前提下,提供完整的文件管理能力。
Security Notes
安全说明
- OAuth tokens are stored securely in your system keyring
- Client secrets are stored in with file permissions 600
~/.config/agent-skills/google-drive.yaml - No passwords are stored - only OAuth tokens
- Tokens refresh automatically when using the skill
- Browser-based consent ensures you approve all requested permissions
Always review OAuth consent screens before granting access to your Google Drive.
- OAuth令牌:安全存储在系统密钥环中
- 客户端密钥:存储在,文件权限设为600
~/.config/agent-skills/google-drive.yaml - 无密码存储:仅存储OAuth令牌,不保存密码
- 令牌自动刷新:使用技能时令牌会自动刷新
- 浏览器授权:所有请求的权限均需你手动确认
在授予Google Drive访问权限前,请务必仔细查看OAuth授权界面的权限说明。