Feishu Document Editor Skill
This skill provides comprehensive guidance for creating and editing Feishu documents using the Feishu OpenAPI.
Prerequisites
1. App Creation and Configuration
Create enterprise self-built app: Login to Feishu Open Platform, create an app and add "Bot" capability.
Apply for API permissions: In "Permission Management", apply for the following permissions:
- - Document editing: INLINECODE0
- Document viewing: INLINECODE1
Publish app: Submit version and publish, ensuring the app coverage includes target users/departments.
2. Get Access Token
Call the self-built app get tenantaccesstoken interface:
CODEBLOCK0
Response example:
CODEBLOCK1
Core Operations
Extract Document ID
Get document_id from document URL:
Example URL: INLINECODE3
document_id = INLINECODE4
Grant App Document Permission
Manually add collaborator:
- - Open document in Feishu client → Click "..." in top right → "More" → "Add document app"
- Search and add your app, grant "Can edit" permission.
Write Text Content
Interface: Create Block
- - Path parameters:
document_id = document ID, block_id = document ID (root node is the document itself) - Request headers:
CODEBLOCK2
- - Request body example (write "hello"):
CODEBLOCK3
Read Document Content
Interface: Get Document Plain Text
CODEBLOCK4
Response example:
CODEBLOCK5
Common Issues
1. Permission Error (403 Forbidden)
- - Diagnosis: App not added as document collaborator, or
tenant_access_token is invalid. - Solution: Re-add app as collaborator, or re-get
tenant_access_token.
2. Missing Access Token (99991661)
- - Diagnosis: Request header does not carry
Authorization: Bearer {token}. - Solution: Ensure
tenant_access_token is correctly added to request header.
3. Document Not Found (404 Not Found)
- - Diagnosis:
document_id is incorrect or document has been deleted. - Solution: Re-extract
document_id from document URL.
Reference Documentation
- - Create Block Interface
- Get Document Plain Text Interface
- Permission Configuration Guide
Through the above steps, you can achieve editing Feishu documents via API, supporting add/delete/modify/query operations for multiple content types including text, tables, and images.
Feishu 文档编辑器技能
本技能提供使用飞书开放API创建和编辑飞书文档的全面指导。
前置条件
1. 应用创建与配置
创建企业自建应用:登录飞书开放平台,创建应用并添加机器人能力。
申请API权限:在权限管理中申请以下权限:
- - 文档编辑:docx:document:write_only
- 文档查看:docx:document:readonly
发布应用:提交版本并发布,确保应用覆盖范围包含目标用户/部门。
2. 获取访问令牌
调用自建应用获取 tenantaccesstoken 接口:
bash
curl -X POST https://open.feishu.cn/open-apis/auth/v3/tenantaccesstoken/internal \
-H Content-Type: application/json \
-d {appid: yourappid, appsecret: yourappsecret}
响应示例:
json
{
code: 0,
tenantaccesstoken: t-xxx,
expire: 7200
}
核心操作
提取文档ID
从文档URL中获取 document_id:
示例URL:https://bigdatacenter.feishu.cn/docx/HpK2dtGu9omhMAxV12zcB6i7ngd
document_id = HpK2dtGu9omhMAxV12zcB6i7ngd
授予应用文档权限
手动添加协作者:
- - 在飞书客户端打开文档 → 点击右上角... → 更多 → 添加文档应用
- 搜索并添加你的应用,授予可编辑权限。
写入文本内容
接口:创建块
- - 路径参数:documentid = 文档ID,blockid = 文档ID(根节点即为文档本身)
- 请求头:
http
Authorization: Bearer {tenant
accesstoken}
Content-Type: application/json
json
{
index: -1,
children: [
{
block_type: 2,
text: {
elements: [
{
text_run: {
content: hello
}
}
]
}
}
]
}
读取文档内容
接口:获取文档纯文本
bash
curl -X GET https://open.feishu.cn/open-apis/docx/v1/documents/{document_id}/plaintext \
-H Authorization: Bearer {tenantaccesstoken}
响应示例:
json
{
code: 0,
data: {
content: 文档文本内容在此
}
}
常见问题
1. 权限错误(403 Forbidden)
- - 诊断:应用未添加为文档协作者,或 tenantaccesstoken 无效。
- 解决方案:重新将应用添加为协作者,或重新获取 tenantaccesstoken。
2. 缺少访问令牌(99991661)
- - 诊断:请求头未携带 Authorization: Bearer {token}。
- 解决方案:确保在请求头中正确添加 tenantaccesstoken。
3. 文档未找到(404 Not Found)
- - 诊断:documentid 错误或文档已被删除。
- 解决方案:从文档URL中重新提取 documentid。
参考文档
通过以上步骤,你可以实现通过API编辑飞书文档,支持文本、表格、图片等多种内容类型的增删改查操作。