Can we use USER_TOKEN in all traccar api as an authorisation

shahab2 years ago

Hello everyone,
I am using traccar v5.6 and i am building a system in which first i will add device to traccar and then takes its device_Id and store in my custom db.
as per this post https://www.traccar.org/forums/topic/unable-to-get-devices-and-add-devices-through-through-api-using-token-in-traccar-412/ i understand that you just can use token api call but i am using v5.6 so is there any changes or not.
i want to know can we use USER_TOKEN in all traccar api(add device or add users) as an authentication/authorisation method or we have to use basic auth for that.

Anton Tananaev2 years ago

You can use the token for API calls.

shahab2 years ago

Thanks Anton for confirming but when i tried it, I was getting 401 error
this is my api call from postman

const axios = require('axios');
let data = JSON.stringify({
"name": "shahasasab",
"uniqueId": "090990909",
"category": "Vehicle"
});

let config = {
method: 'post',
maxBodyLength: Infinity,
url: 'http://localhost:8082/api/devices?token=USER_TOKEN',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
data : data
};

axios.request(config)
.then((response) => {
console.log(JSON.stringify(response.data));
})
.catch((error) => {
console.log(error);
});

But when i use basic auth instead of token then api is working fine so can you please tell me what is wrong with it or i have to add something

Anton Tananaev2 years ago

You have to use it as a Bearer token in the Authorization header, not as a query parameter.

shahab2 years ago

Thank you Anton, its work