Unable to get devices and add devices through through api using token in traccar 4.12

Waqar2 years ago

When I try to get devices using my token it give an unauthorized error
but when I called it from the web where traccar is configured it uses some Jsession id i think
when I get JSESSONID from api it doesn't work

Anton Tananaev2 years ago

Please provide more details on how you're trying to use it.

Waqar2 years ago
   $curl = curl_init();

   curl_setopt_array($curl, array(
      CURLOPT_URL => 'http://traccar.example.com/api/devices?token=8ZnQ1vU6xKxLqQIW6fLXVZw6KdXCkYQS',
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => '',
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 0,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => 'GET',
      CURLOPT_HTTPHEADER => array(
         'Content-Type: application/json',
      ),
   ));

   $response = curl_exec($curl);

   curl_close($curl);
   echo $response;
Waqar2 years ago

Response:

HTTP 401 Unauthorized - WebApplicationException (SecurityRequestFilter:113 < ...)
Anton Tananaev2 years ago

Yeah, you can't use the token that way. You have to create a session first.

Waqar2 years ago

can you please share the endpoint to create a session?

Anton Tananaev2 years ago

Have you read our API documentation?

Waqar2 years ago

yeah but my version is 4.12 in new docs they said to use username:pass but can we do that with out basic auth?

Anton Tananaev2 years ago
Waqar2 years ago

yes, I call the endpoint of api/session?token=8ZnQ1vU6xKxLqQIW6fLXVZw6KdXCkYQS

I got this response

{"id":4,"attributes":{},"name":"test.allotrac","login":"","email":"test@allotrac.com.au","phone":"","readonly":false,"administrator":false,"map":"","latitude":0.0,"longitude":0.0,"zoom":0,"twelveHourFormat":false,"coordinateFormat":"","disabled":false,"expirationTime":null,"deviceLimit":-1,"userLimit":0,"deviceReadonly":false,"token":"8ZnQ1vU6xKxLqQIW6fLXVZw6KdXCkYQS","limitCommands":false,"poiLayer":"","password":null}

Now what to do?

Anton Tananaev2 years ago

Then you use the session cookie for your next requests.

Waqar2 years ago

Do you mean JSESSIONID which is in the response of the server header?

Anton Tananaev2 years ago

Yes.

Waqar2 years ago

Hello,
We had some stuff back from google today regarding an indexing issue from a link as I posted the full link of the site and there is no option to delete or edit my comment.
Can you please help me to edit or delete this to solve my problem?
Thanks

Anton Tananaev2 years ago

Changed the link.