get token for embed

Ledz2 years ago

how to get token on new version? i cant get token anymore. i cant find it

$auth = base64_encode("test:test");
    $url = "http://192.168.100.1:8082/api/session";
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

    $headers = array(
       "Accept: application/json",
       "Authorization: Basic $auth",
       "Content-Type: application/x-www-form-urlencoded;charset=UTF-8",
    );
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    $data = "email=test&password=test25";
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    $res = curl_exec($curl);
    curl_close($curl);
henry beltran2 years ago

version 5.3 this work for me

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'http://192.168.100.1:8082/api/session/token',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS => 'expiration=2022-09-11T22%3A53%3A56-04%3A00',
  CURLOPT_HTTPHEADER => array(
    'Authorization: Basic dGVzdCB0ZXN0dGVzdCB0ZXN0dGVzdCB0ZXN0',
    'Content-Type: application/x-www-form-urlencoded'
  ),
));

$response = curl_exec($curl);

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

that worked!! thanks

Hans Strassguetl2 years ago

Hi Henry,

how did you resolve to find the CURLOPT_POSTFIELDS content of expiration= ?

I was not able to find anything related to /api/session/token in the api reference.

Thanks in advance.

henry beltran2 years ago

I had problems with the date format, if you use the previous php code you can define access to the platform using a token, obviously you must change the credentials corresponding to the user you want to give access to. The token lifetime date format must be in ISO-8601. If the date format is incorrect, a token valid for 7 days is generated by default. You can still generate token with modern web GUI

Hans Strassguetl2 years ago

Thanks for your reply.

I'm still on 5.2 so maybe my problem is related to that.
Nevertheless I'm missing the documentation: https://www.traccar.org/api-reference/#tag/Session

Anton Tananaev2 years ago

You should upgrade first. Contributions to the documentation are welcome.

Hans Strassguetl2 years ago

Thanks Anton, you're right, I needed to updgrade first.
It took a little while with my answer as I had to set up a test environment for v 5.4 - and now it works.

Here is a little python code:

import requests
from requests.auth import HTTPBasicAuth

if __name__ == "__main__":
    '''
    ----------------------------------------------------------
    YOU SET
        * your URL to access traccar
        * Your credentials for your traccar account
        * Set end date for the token you want to receive
    
    You RECEIVE 
        * A token.txt file.       
    
    This token you can use for calling your traccar. Example:            
    http://192.168.58.230:8082/?token=RjBEAiBP_x3HnQeRq9R                
    
    This sessions should need a re-signin after given date you set.        
    ---------------------------------------------------------- '''
    
    
    ''' ------------------------------------------------------ '''
    ''' Change to your data 								   '''
    '''                                                        '''
    url = 'http://192.168.18.230:8082/api/session/token'                    # Set your URL
    my_auth = HTTPBasicAuth('your_email', 'your_password')         			# set your credentials
    payload = {'expiration': '2029-12-31T23:59:59Z' }                       # set your expected expiration date
    ''' ------------------------------------------------------ '''

    my_whatever_filename = 'token.txt'
    headers = {'Content-Type': 'application/x-www-form-urlencoded'}
    # get data from traccar
    data_from_traccar = requests.post(url,  params=payload, auth=my_auth, headers=headers)		    		   

    # if rc is not 200 then something is wrong!
    if data_from_traccar.status_code != 200:
        print('Error!')														
        print(str(data_from_traccar.status_code))
        print(str(data_from_traccar.content))
    else:
        with open(my_whatever_filename, 'wb') as f:										
            f.write(data_from_traccar.content)
        print(' ')
        print(' ')
        print('You have a new session token in your ' + my_whatever_filename)