401 Unauthorized Error REST API

Status
Not open for further replies.

Rpuh

SOHO User
Joined
May 20, 2024
Messages
4
Reaction score
0
Hello,

I am trying to programmatically change the "internal calls forwarding rules" settings using the 3CX REST API, but my server constantly returns a 401 Unauthorized status code. I have verified that the API token is copied directly from the 3CX web platform. I am using the Node.js framework for web requests and have also tried using tools like curl, but I am encountering the same issue.

Does anyone have any idea what might be wrong?

Here is an example of my code:


const axios = require('axios');

const apiBaseUrl = 'http://myserver/api';
const apiToken = 'myToken';

async function checkAuthentication() {
try {
const response = await axios.get(`${apiBaseUrl}/`, {
headers: {
'Authorization': `Bearer ${apiToken}`,
'Content-Type': 'application/json',
}
});
if (response.status === 200) {
console.log('Successfully authenticated to 3CX server');
} else {
console.log('Failed to authenticate to 3CX server');
}
} catch (error) {
if (error.response && error.response.status === 401) {
console.log('Authentication failed: Invalid API token or credentials');
console.log('Response from server:', error.response.data);
} else {
console.error('Error connecting to 3CX server:', error.message);
if (error.response) {
console.error('Server responded with:', error.response.data);
}
}
}
}

checkAuthentication();

Thank you in advance for your assistance!
 
V20 or V18?
 
V18 uses a session cookie and not an access token like the new XAPI (the Webclient's Admin backend.)
 
V18 uses a session cookie and not an access token like the new XAPI (the Webclient's Admin backend.)
Thank you, I have successfully authenticated. Now I have a second question regarding API endpoints. For the last 4 hours, I have been trying to figure out how to change the forwarding rules for "external calls" for a specific number when the status is set to "away," but without luck. It has been really painful. Is there any official documentation where I can look these up?

I would like a function which sets the forwarding number of "extensionNumber" when the status is set to "away." Can anyone provide guidance or point me to the relevant documentation?
 
Thank you, I have successfully authenticated. Now I have a second question regarding API endpoints. For the last 4 hours, I have been trying to figure out how to change the forwarding rules for "external calls" for a specific number when the status is set to "away," but without luck. It has been really painful. Is there any official documentation where I can look these up?

I would like a function which sets the forwarding number of "extensionNumber" when the status is set to "away." Can anyone provide guidance or point me to the relevant documentation?
I recommend upgrading to v20 and playing around with the XAPI. It's much more complete and easier to work with.

https://<FQDN>:<PORT>/xapi/v1/swagger.yaml
 
Status
Not open for further replies.

Latest Posts

Forum statistics

Threads
111,893
Messages
589,598
Members
164,763
Latest member
Techmansam