Feishishu document operations using useru User
Fe access token authentication. Call Feishu Open API directly via REST API.
Install Dependencies
CODEBLOCK0
Quick Start
CODEBLOCK1
Get User Access Token
Step 1: Get App Credentials from Feishu Open Platform
Prepare the following:
- - APPID - App ID (from Feishu Open Platform app settings)
- APPSECRET - App Secret (from Feishu Open Platform app settings)
- REDIRECT_URI - Authorization callback URL
Enable these permissions:
- -
docx:document - Document operations - INLINECODE1 - Cloud drive search
- INLINECODE2 - Document search
Step 2: Generate Authorization URL
CODEBLOCK2
Step 3: Exchange for Token
CODEBLOCK3
The returned access_token is your user_access_token.
Usage Examples
CODEBLOCK4
Convenience Functions
Don't want to create a client? Use functions directly:
CODEBLOCK5
API Reference
FeishuClient
| Method | Description |
|---|
| INLINECODE5 | Read document content |
| INLINECODE6 |
Create new document |
|
write_doc(doc_token, content) | Write document (overwrite) |
|
append_doc(doc_token, content) | Append content to end |
|
list_blocks(doc_token) | List all blocks |
|
get_block(doc_token, block_id) | Get specific block |
|
update_block(doc_token, block_id, content) | Update block content |
|
delete_block(doc_token, block_id) | Delete block |
Notes
- 1.
user_access_token has an expiration time, needs periodic refresh - The
scope in authorization URL must be enabled in Feishu Open Platform - This skill accesses personal cloud documents using user identity
Related Links
- - Feishu Open Platform: https://open.feishu.cn
- Document API: https://open.feishu.cn/document/ukTMukTMukTM/uADOwUjLwgDMzCM4ATm
Token Auto Refresh
Use feishu_token.py script for automatic token refresh.
Install Dependencies
CODEBLOCK6
First Authorization
CODEBLOCK7
After user authorizes, will callback to INLINECODE16
CODEBLOCK8
Token is automatically saved to INLINECODE17
Refresh Token
CODEBLOCK9
In Code
CODEBLOCK10
飞书用户文档操作
使用用户访问令牌认证。通过REST API直接调用飞书开放API。
安装依赖
bash
pip install requests
快速开始
python
from feishu_client import FeishuClient
初始化客户端
client = FeishuClient(user
accesstoken=u-xxx)
获取用户访问令牌
步骤1:从飞书开放平台获取应用凭证
准备以下信息:
- - APPID - 应用ID(来自飞书开放平台应用设置)
- APPSECRET - 应用密钥(来自飞书开放平台应用设置)
- REDIRECT_URI - 授权回调地址
启用以下权限:
- - docx:document - 文档操作
- drive:drive.search:readonly - 云盘搜索
- search:docs:read - 文档搜索
步骤2:生成授权地址
https://accounts.feishu.cn/open-apis/authen/v1/authorize?clientid={YOURAPPID}&responsetype=code&redirecturi={YOURREDIRECT_URI}&scope=docx%3Adocument%20drive%3Adrive.search%3Areadonly%20search%3Adocs%3Aread
步骤3:换取令牌
bash
curl -X POST https://open.feishu.cn/open-apis/authen/v1/access_token \
-H Content-Type: application/json \
-d {
granttype: authorizationcode,
code: {YOUR_CODE},
appid: {YOURAPP_ID},
appsecret: {YOURAPP_SECRET}
}
返回的 accesstoken 即为您的 useraccess_token。
使用示例
python
from feishu_client import FeishuClient
初始化
client = FeishuClient(user
accesstoken=u-xxx)
读取文档
content = client.read
doc(doctoken)
print(content)
创建文档
new
token = client.createdoc(我的新文档)
print(f新文档:{new_token})
写入文档
client.write
doc(doctoken, # 标题\n\n内容)
追加内容
client.append
doc(doctoken, ## 新章节\n\n更多内容)
列出所有块
blocks = client.list
blocks(doctoken)
for block in blocks:
print(block)
获取特定块
block = client.get
block(doctoken, block_id)
更新块
client.update
block(doctoken, block_id, 新内容)
删除块
client.delete
block(doctoken, block_id)
便捷函数
不想创建客户端?直接使用函数:
python
from feishuclient import readdocument, createdocument, writedocument, append_document
读取
content = read
document(doctoken, user
accesstoken=u-xxx)
创建
new
token = createdocument(标题, user
accesstoken=u-xxx)
写入
write
document(doctoken, # 内容, user
accesstoken=u-xxx)
追加
append
document(doctoken, ## 更多, user
accesstoken=u-xxx)
API参考
FeishuClient
| 方法 | 描述 |
|---|
| readdoc(doctoken) | 读取文档内容 |
| createdoc(title, foldertoken) |
创建新文档 |
| write
doc(doctoken, content) | 写入文档(覆盖) |
| append
doc(doctoken, content) | 在末尾追加内容 |
| list
blocks(doctoken) | 列出所有块 |
| get
block(doctoken, block_id) | 获取特定块 |
| update
block(doctoken, block_id, content) | 更新块内容 |
| delete
block(doctoken, block_id) | 删除块 |
注意事项
- 1. useraccesstoken 有有效期,需要定期刷新
- 授权地址中的 scope 必须在飞书开放平台中启用
- 本技能使用用户身份访问个人云文档
相关链接
- - 飞书开放平台:https://open.feishu.cn
- 文档API:https://open.feishu.cn/document/ukTMukTMukTM/uADOwUjLwgDMzCM4ATm
令牌自动刷新
使用 feishu_token.py 脚本实现令牌自动刷新。
安装依赖
bash
pip install requests
首次授权
bash
1. 生成授权地址
python feishu
token.py --app-id YOURAPP
ID --app-secret YOURSECRET --redirect-uri YOUR
REDIRECTURI --url
用户授权后,将回调到 YOURREDIRECTURI?code=XXX
bash
2. 使用授权码获取令牌
python feishu
token.py --app-id YOURAPP
ID --app-secret YOURSECRET --code AUTH_CODE
令牌自动保存到 ~/.config/claw-feishu-user/config.json
刷新令牌
bash
python feishutoken.py --app-id YOURAPPID --app-secret YOURSECRET --refresh
在代码中使用
python
import json
import os
读取缓存的令牌
config_path = os.path.expanduser(~/.config/claw-feishu-user/config.json)
with open(config_path) as f:
config = json.load(f)
使用令牌
client = FeishuClient(user
accesstoken=config[access_token])