84 lines
2.4 KiB
Python
84 lines
2.4 KiB
Python
#!/usr/bin/env python3
|
||
"""
|
||
使用飞书旧版 API 创建云文档
|
||
"""
|
||
import requests
|
||
import json
|
||
import sys
|
||
|
||
# 飞书应用凭证
|
||
APP_ID = "cli_a93815b250b9dcb5"
|
||
APP_SECRET = "NogaPY8DiMHMyadOKDW26bqkGPnrOkND"
|
||
|
||
def get_tenant_access_token():
|
||
"""获取飞书 tenant_access_token"""
|
||
url = "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal"
|
||
payload = {
|
||
"app_id": APP_ID,
|
||
"app_secret": APP_SECRET
|
||
}
|
||
resp = requests.post(url, json=payload, timeout=10)
|
||
data = resp.json()
|
||
if data.get("code") != 0:
|
||
raise Exception(f"获取 access_token 失败:{data}")
|
||
return data["tenant_access_token"]
|
||
|
||
def create_document_legacy(token, title, content):
|
||
"""使用旧版 API 创建飞书云文档"""
|
||
# 旧版 API:创建文档
|
||
url = "https://open.feishu.cn/open-apis/doc/v1/documents"
|
||
headers = {
|
||
"Authorization": f"Bearer {token}",
|
||
"Content-Type": "application/json"
|
||
}
|
||
payload = {
|
||
"title": title,
|
||
"content": content,
|
||
}
|
||
resp = requests.post(url, json=payload, headers=headers, timeout=10)
|
||
print(f"HTTP 状态码:{resp.status_code}")
|
||
print(f"响应内容:{resp.text[:500]}")
|
||
try:
|
||
data = resp.json()
|
||
except:
|
||
data = {"raw": resp.text}
|
||
print(f"解析后:{data}")
|
||
if isinstance(data, dict) and data.get("code") != 0:
|
||
raise Exception(f"创建文档失败:{data}")
|
||
|
||
doc_token = data["data"]["doc_token"]
|
||
|
||
return doc_token
|
||
|
||
def main():
|
||
if len(sys.argv) < 3:
|
||
print("用法:python3 create_feishu_doc_legacy.py <title> <content_file>")
|
||
sys.exit(1)
|
||
|
||
title = sys.argv[1]
|
||
content_file = sys.argv[2]
|
||
|
||
# 读取文档内容
|
||
with open(content_file, 'r', encoding='utf-8') as f:
|
||
content = f.read()
|
||
|
||
print(f"创建文档:{title}")
|
||
print(f"内容来源:{content_file}")
|
||
|
||
# 获取访问令牌
|
||
print("正在获取访问令牌...")
|
||
token = get_tenant_access_token()
|
||
print("✓ 获取访问令牌成功")
|
||
|
||
# 创建文档
|
||
print("正在创建文档(使用旧版 API)...")
|
||
doc_token = create_document_legacy(token, title, content)
|
||
print(f"✓ 文档创建成功")
|
||
print(f"文档 Token: {doc_token}")
|
||
print(f"文档 URL: https://www.feishu.cn/docx/{doc_token}")
|
||
|
||
return doc_token
|
||
|
||
if __name__ == "__main__":
|
||
main()
|