Authenticate a user
This example demonstrates how to authenticate a user in ONLYOFFICE DocSpace using the API and check the authentication with a token received.
Before you start
Replace https://yourportal.onlyoffice.com with your actual DocSpace portal URL, replace USER_CREDENTIALS with your credentials.
Full example
- Node.js
- Python
// Set API base URL
const BASE_URL = 'https://yourportal.onlyoffice.com';
// User credentials for authentication
const USER_CREDENTIALS = {
userName: 'your-user-name',
password: 'your-password',
};
// Step 1: Authenticate with your login and password
function authenticate() {
return fetch(`${BASE_URL}/api/2.0/authentication`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(USER_CREDENTIALS),
})
.then((res) => {
if (res.status === 200) return res.json();
const text = await res.text();
console.log(`Authentication failed. Status code: ${res.status}, Message: ${text}`);
return null;
})
.then((data) => {
if (!data) return null;
const authToken = data?.response?.token;
return authToken || null;
})
.catch((err) => {
console.log(`Authentication error: ${err}`);
return null;
});
}
// Step 2: Check authentication with a token you received
function checkAuthentication(token) {
return fetch(`${BASE_URL}/api/2.0/authentication`, {
method: 'GET',
headers: { Authorization: token },
})
.then((res) => {
if (res.status === 200) {
console.log(`User is authenticated with token ${token}.`);
} else {
const text = await res.text();
console.log(`Authentication check failed. Status code: ${res.status}, Message: ${text}`);
}
})
.catch((err) => {
console.log(`Authentication check error: ${err}`);
});
}
// Run
authenticate().then((token) => {
if (token) {
checkAuthentication(token);
}
});
import requests
# Set API base URL
BASE_URL = 'https://yourportal.onlyoffice.com'
# User credentials for authentication
USER_CREDENTIALS = {
'userName': 'your-user-name',
'password': 'your-password',
}
# Step 1: Authenticate with your login and password
def authenticate():
response = requests.post(f'{BASE_URL}/api/2.0/authentication', json=USER_CREDENTIALS)
if response.status_code == 200:
auth_token = response.json().get('response', {}).get('token')
if auth_token:
return auth_token
else:
print(f'Authentication failed. Status code: {response.status_code}, Message: {response.text}')
return None
# Step 2: Check authentication with a token you received
def check_authentication(token):
headers = {'Authorization': token}
response = requests.get(f'{BASE_URL}/api/2.0/authentication', headers=headers)
if response.status_code == 200:
print(f'User is authenticated with token {token}.')
else:
print(f'Authentication check failed. Status code: {response.status_code}, Message: {response.text}')
if __name__ == '__main__':
# Step 1
token = authenticate()
if token:
# Step 2
check_authentication(token)
Step 1: Authenticate a user
A POST request is sent to /api/2.0/authentication to authenticate with USER_CREDENTIALS.
- Node.js
- Python
function authenticate() {
return fetch(`${BASE_URL}/api/2.0/authentication`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(USER_CREDENTIALS),
})
.then((res) => {
if (res.status === 200) return res.json();
const text = await res.text();
console.log(`Authentication failed. Status code: ${res.status}, Message: ${text}`);
return null;
})
.then((data) => {
if (!data) return null;
const authToken = data?.response?.token;
return authToken || null;
})
.catch((err) => {
console.log(`Authentication error: ${err}`);
return null;
});
}
def authenticate():
response = requests.post(f'{BASE_URL}/api/2.0/authentication', json=USER_CREDENTIALS)
if response.status_code == 200:
auth_token = response.json().get('response', {}).get('token')
if auth_token:
return auth_token
else:
print(f'Authentication failed. Status code: {response.status_code}, Message: {response.text}')
return None
Step 2: Check authentication
A GET request is sent to /api/2.0/authentication to check authentication success.
- Node.js
- Python
function checkAuthentication(token) {
return fetch(`${BASE_URL}/api/2.0/authentication`, {
method: 'GET',
headers: { Authorization: token },
})
.then((res) => {
if (res.status === 200) {
console.log(`User is authenticated with token ${token}.`);
} else {
const text = await res.text();
console.log(`Authentication check failed. Status code: ${res.status}, Message: ${text}`);
}
})
.catch((err) => {
console.log(`Authentication check error: ${err}`);
});
}
def check_authentication(token):
headers = {'Authorization': token}
response = requests.get(f'{BASE_URL}/api/2.0/authentication', headers=headers)
if response.status_code == 200:
print(f'User is authenticated with token {token}.')
else:
print(f'Authentication check failed. Status code: {response.status_code}, Message: {response.text}')