深入解析:如何将Notion用户添加到ONLYOFFICE协作空间
在当今的混合工作模式下,团队常常使用多种工具。Notion 作为强大的知识库和项目管理中心,而 ONLYOFFICE 则提供了卓越的文档创建与协作体验。然而,在两个平台间手动添加和管理用户,不仅耗时耗力,还容易出错。
为了解决这一痛点,我们开发了一个自动化脚本,能够智能地检索您的 Notion 工作区成员,并一键将他们同步至 ONLYOFFICE 协作空间。本文将详细介绍如何利用这个脚本,确保您的整个团队无缝衔接,实现从构思到协作文档的无缝流转。

关于 ONLYOFFICE 协作空间
ONLYOFFICE 协作空间是一个协同办公平台,能够帮助用户更好地与客户、业务合作伙伴、承包商及第三方,进行文档、表格、幻灯片、PDF 和表单的在线编辑与协作。设置灵活的访问权限和用户角色设置,可支持用户对整个或单独房间的访问权限调整。
开发者版是我们开源解决方案的商业版本,主要用途是集成至您自有的商业软件和服务器。这一版本还支持自定义品牌选项、连接外部服务和存储等。查看视频了解更多信息:
什么是开发者版 ONLYOFFICE 协作空间?无缝集成至您的软件和服务器
Notion 简介
在深入探讨如何自动化集成之前,我们首先需要理解集成的核心之一:Notion。Notion 是一款极其流行的“一体化工作空间”工具,它以其高度的灵活性和强大的功能而闻名。您可以将它视为一个数字瑞士军刀,团队可以用它来完成各式各样的任务:
项目与任务管理: 创建待办事项列表、跟踪项目进度和使用看板视图。
知识库与文档: 构建团队Wiki、记录会议纪要和编写产品需求文档。
数据库与协作: 创建可关联、可筛选的数据库,来管理客户信息、产品路线图等,并允许团队成员在页面内实时评论和协作。
如何将Notion用户添加到ONLYOFFICE协作空间
工作原理
- 检索 Notion 用户 - 获取用户列表,包括其姓名和电子邮件地址。
- 检查协作空间用户 - 对于每个 Notion 用户,通过电子邮件验证其是否已存在于协作空间中。
- 创建缺失的协作空间用户 - 如果协作空间中缺少某个 Notion 用户,则使用默认密码创建其帐户。
完整代码
import requests
# Notion API
NOTION_TOKEN = "your-notion-token"
NOTION_HEADERS = {
"Authorization": f"Bearer {NOTION_TOKEN}",
"Notion-Version": "2022-06-28",
"Content-Type": "application/json"
}
# 协作空间 API
ONLYOFFICE_API_HOST = "your-docspace.onlyoffice.com"
ONLYOFFICE_API_KEY = "your_onlyoffice_api_key"
# 协作空间身份验证
ONLYOFFICE_HEADERS = {
"Accept": "application/json",
"Authorization": f"Bearer {ONLYOFFICE_API_KEY}",
"Content-Type": "application/json"
}
# 步骤 1:从 Notion 获取所有用户
def get_notion_users():
url = "https://api.notion.com/v1/users"
response = requests.get(url, headers=NOTION_HEADERS)
if response.status_code == 200:
return response.json()["results"]
else:
print("Failed to fetch Notion users:", response.text)
return []
# 步骤 2:通过电子邮件检查用户是否存在于协作空间中
def check_docspace_user(email):
url = f"https://{ONLYOFFICE_API_HOST}/api/2.0/people/email?email={email}"
response = requests.get(url, headers=ONLYOFFICE_HEADERS)
return response.status_code == 200
# 步骤 3:将缺失的用户添加到协作空间
def add_docspace_user(first_name, last_name, email):
url = f"https://{ONLYOFFICE_API_HOST}/api/2.0/people"
data = {
"password": "defaultpassword123",
"firstName": first_name,
"lastName": last_name,
"email": email
}
response = requests.post(url, json=data, headers=ONLYOFFICE_HEADERS)
if response.status_code == 200:
print(f"User {email} added to DocSpace!")
else:
print(f"Failed to add {email}: {response.text}")
# 主执行循环
if __name__ == "__main__":
notion_users = get_notion_users()
for user in notion_users:
if user["type"] == "person":
name = user["name"]
email = user.get("person", {}).get("email")
if email:
if not check_docspace_user(email):
if " " in name:
first_name, last_name = name.split(" ", 1)
else:
first_name, last_name = name, "-"
add_docspace_user(first_name, last_name, email)
准备工作
- 请将 your-docspace.onlyoffice.com 和 your_onlyoffice_api_key 替换为您的实际协作空间门户主机和 API 密钥,确保您拥有执行用户操作所需的权限。
- 在向 API 发出请求之前,您需要创建一个协作空间 API 密钥。
- 创建一个新的 Notion 集成以获取 API 令牌,并用它替换 your-notion-token。提交后,您将看到一个内部集成令牌。请确认已勾选“读取内容”和“读取用户信息”功能。
步骤 1:检索 Notion 用户
使用 Notion API GET api.notion.com/v1/users 请求从 Notion 获取用户。筛选类型为“人员”的条目,以获取每个用户的姓名和电子邮件地址。
# 步骤 1:从 Notion 获取所有用户
def get_notion_users():
url = "https://api.notion.com/v1/users"
response = requests.get(url, headers=NOTION_HEADERS)
if response.status_code == 200:
return response.json()["results"]
else:
print("Failed to fetch Notion users:", response.text)
return []
if __name__ == "__main__":
notion_users = get_notion_users()
for user in notion_users:
if user["type"] == "person":
name = user["name"]
email = user.get("person", {}).get("email")
步骤 2:检查协作空间中是否存在用户
使用 GET /api/2.0/people/email?email={email} 请求检查协作空间中是否存在用户。
# 步骤 2:通过电子邮件检查用户是否存在于协作空间中
def check_docspace_user(email):
url = f"https://{ONLYOFFICE_API_HOST}/api/2.0/people/email?email={email}"
response = requests.get(url, headers=ONLYOFFICE_HEADERS)
return response.status_code == 200
# 主执行循环
if __name__ == "__main__":
notion_users = get_notion_users()
for user in notion_users:
if user["type"] == "person":
name = user["name"]
email = user.get("person", {}).get("email")
if email:
if not check_docspace_user(email):
# 如果协作空间中缺少用户
pass
步骤 3:将缺失用户添加到协作空间
使用 POST /api/2.0/people 请求,使用 Notion 数据将缺失用户添加到协作空间。
# 步骤 3:将缺失的用户添加到协作空间
def add_docspace_user(first_name, last_name, email):
url = f"https://{ONLYOFFICE_API_HOST}/api/2.0/people"
data = {
"password": "defaultpassword123", # Replace with any temporary password or generate unique
"firstName": first_name,
"lastName": last_name,
"email": email
}
response = requests.post(url, json=data, headers=ONLYOFFICE_HEADERS)
if response.status_code == 200:
print(f"User {email} added to DocSpace!")
else:
print(f"Failed to add {email}: {response.text}")
# 主执行循环
if __name__ == "__main__":
notion_users = get_notion_users()
for user in notion_users:
if user["type"] == "person":
name = user["name"]
email = user.get("person", {}).get("email")
if email:
if not check_docspace_user(email):
if " " in name:
first_name, last_name = name.split(" ", 1)
else:
first_name, last_name = name, "-"
add_docspace_user(first_name, last_name, email)
小结
这个简单的自动化脚本消除了在 Notion 和 ONLYOFFICE 之间手动管理用户的痛点,确保了您的协作空间始终与您的团队结构保持同步。通过实现用户管理的自动化,您不仅提升了管理效率,更保障了团队协作的流畅性。现在就动手集成,让您的团队专注于创造内容,而不是管理工具。
如果您在使用协作空间插件时有任何疑问,欢迎前往 ONLYOFFICE 论坛向我们的开发团队提问。您也可以通过 GitHub 提交问题,反馈功能需求或报告错误。

浙公网安备 33010602011771号