跳到主要内容

Add Notion users to ONLYOFFICE Apps

This script automates the process of retrieving Notion users and adding them to ONLYOFFICE Apps. It ensures that all Notion team members are onboarded into ONLYOFFICE Apps.

How it works​

  • Retrieve Notion users - Fetches a list of users with their names and emails.
  • Check ONLYOFFICE Apps users - For each Notion user, verifies if they already exist in ONLYOFFICE Apps by email.
  • Create missing ONLYOFFICE Apps users - If a Notion user is missing in ONLYOFFICE Apps, creates their account with a default password.
Full example
import requests

# Notion API credentials
NOTION_TOKEN = "your-notion-token"
NOTION_HEADERS = {
"Authorization": f"Bearer {NOTION_TOKEN}",
"Notion-Version": "2022-06-28",
"Content-Type": "application/json"
}

# ONLYOFFICE Apps API credentials
ONLYOFFICE_API_HOST = "your-docspace.onlyoffice.com"
ONLYOFFICE_API_KEY = "your_onlyoffice_api_key"

# Headers for the ONLYOFFICE Apps authentication
ONLYOFFICE_HEADERS = {
"Accept": "application/json",
"Authorization": f"Bearer {ONLYOFFICE_API_KEY}",
"Content-Type": "application/json"
}

# Step 1: Get all users from 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 []

# Step 2: Check if user exists in ONLYOFFICE Apps by email
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

# Step 3: Add missing users to ONLYOFFICE Apps
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 ONLYOFFICE Apps!")
else:
print(f"Failed to add {email}: {response.text}")

# Main execution loop
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)

Before you start​

  1. Replace your-docspace.onlyoffice.com and your_onlyoffice_api_key with your actual ONLYOFFICE Apps portal host and API key. Ensure you have the necessary permissions to perform user operations.
  2. Before you can make requests to the API, you need to create an ONLYOFFICE Apps API key. Check out the API keys article to learn how to obtain and use API keys.
  3. Create a new Notion integration to obtain an API token and replace your-notion-token with it. After submitting, you'll see an Internal Integration Token. Verify that the Read content and Read user information capabilities are checked.

Step 1: Retrieve Notion users​

Use a Notion API GET api.notion.com/v1/users request to get users from Notion. Filter for entries where the type is person to get name and email for each user.

# Step 1: Get all users from 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")

Step 2: Check if a user exists in ONLYOFFICE Apps​

Use a GET /api/2.0/people/email?email={email} request to check if the user exists in ONLYOFFICE Apps.

# Step 2: Check if user exists in ONLYOFFICE Apps by email
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

# Main execution loop
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 user is missing in ONLYOFFICE Apps
pass

Step 3: Add missing users to ONLYOFFICE Apps​

Use a POST /api/2.0/people request to add missing users to ONLYOFFICE Apps using Notion data.

# Step 3: Add missing users to ONLYOFFICE Apps
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 ONLYOFFICE Apps!")
else:
print(f"Failed to add {email}: {response.text}")

# Main execution loop
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)