You are not logged in.

Announcement

 Téléchargez la dernière version stable de GLPI      -     Et vous, que pouvez vous faire pour le projet GLPI ? :  Contribuer
 Download last stable version of GLPI                      -     What can you do for GLPI ? :  Contribute

#1 2022-06-21 16:58:21

lucaszephyr
Member
Registered: 2022-04-25
Posts: 29

error 'ERROR_SESSION_TOKEN_MISSING' api rest.

I want to open a call using the glpi REST API, but I have some difficulties.

I enabled the REST API. I have the App-token, user_token and session_token. but at the end of the PHP code I have an error of "ERROR_SESSION_TOKEN_MISSING".

Here's the code:



<?php
include '../../../inc/includes.php';

$api_url="http://localhost/glpi_pcpa/apirest.php";
    $app_token="qHGjkP5EZ72al2uH0kANxrVk8iTY6L0hW6I6LwaZ";
    $user_token = "9paN3NW7bfDeGfVplYN01xKi6Qhy5sD2wG5cQIWq";
    $ch = curl_init();
    $url=$api_url . "/initSession?Content-Type=%20application/json&App_Token=".$app_token."&user_token=".$user_token;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $json = curl_exec($ch);
    curl_close ($ch);
    $obj = json_decode($json,true);              //{"session_token":"qjr28v40kb88tucrpli2n0nupt"}
   
    $sess_token = $obj['session_token'];
    $headers = array(
        'Content-Type: application/json',
        "App-Token: " => $app_token,
        //"user_token: " => $user_token,
        "Session-Token: " => $sess_token
        );

$ch = curl_init();
    $url=$api_url . "/Ticket/";
    $input='{ "input": {
        "name": "ticket-IbV01",
        "content": "glpi_array-ticket",
                        "status":"3",
                        "urgency":"1",
                        "priority":"4",
                        "resquesttypes_id":"5",
                        "itilcategories_id":"93",
                        "_users_id_recipient":"2"
    }
    }';
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $input);
    $request_result = curl_exec($ch);
    curl_close ($ch);

    $obj = json_decode($request_result,true);

var_dump($obj);
?>


When I generate the code I get the following error:

array(2) { [0]=> string(27) "ERROR_SESSION_TOKEN_MISSING" [1]=> string(158) "the session_token parameter is missing or empty; see documentation in your browser at http://localhost/glpi_pcpa/apirest.php/#ERROR_SESSION_TOKEN_MISSING"}



What's my error?

Offline

#2 2022-06-21 17:24:56

LaDenrée
HELPER
Registered: 2012-11-19
Posts: 6,287

Re: error 'ERROR_SESSION_TOKEN_MISSING' api rest.

moved to API section.

It looks strange, I don't see any differences with my script and it works on My 9.5.5 instance and 9.4.6 also.

API is working since you get session token with init session.

can you GET some items ?


Trouver la panne avant de réparer...
GLPI10.0.16 (ubuntu 22.04 PHP8.1  Mariadb10.6 ) plugins : comportements 2.7.3 reports 1.16.0 formcreator 2.13.9, datainjection 2.13.5 fields 1.21.9

Offline

#3 2022-06-21 18:56:15

lucaszephyr
Member
Registered: 2022-04-25
Posts: 29

Re: error 'ERROR_SESSION_TOKEN_MISSING' api rest.

version: 9.5.6
Is there a problem with my version?

Do you want a specific items?

Offline

#4 2022-06-21 22:46:44

LaDenrée
HELPER
Registered: 2012-11-19
Posts: 6,287

Re: error 'ERROR_SESSION_TOKEN_MISSING' api rest.

set your headers as this :

    $headers = array(
        'Content-Type: application/json',
        "App-Token: " . $app_token,
        "Session-Token: " . $sess_token
        );

(array of 3 strings, not as keys=>value)


Trouver la panne avant de réparer...
GLPI10.0.16 (ubuntu 22.04 PHP8.1  Mariadb10.6 ) plugins : comportements 2.7.3 reports 1.16.0 formcreator 2.13.9, datainjection 2.13.5 fields 1.21.9

Offline

#5 2022-06-22 21:03:23

lucaszephyr
Member
Registered: 2022-04-25
Posts: 29

Re: error 'ERROR_SESSION_TOKEN_MISSING' api rest.

LaDenrée wrote:

set your headers as this :

    $headers = array(
        'Content-Type: application/json',
        "App-Token: " . $app_token,
        "Session-Token: " . $sess_token
        );

(array of 3 strings, not as keys=>value)


My error was the exchange of token-app: I generated a new token and didn't update the code. Thank you for your help!

Offline

Board footer

Powered by FluxBB