SF 5.4.9 - Consommer une api REST GLPI
Bonjour,
j'aimerais consommer une api REST qui à la documentation suivante :
Pièce jointe 621090
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| Init session
URL: apirest.php/initSession/
Description: Request a session token to uses other API endpoints.
Method: GET
Parameters: (Headers)
App-Token: authorization string provided by the GLPI API configuration. Optional.
a couple login & password: 2 parameters to login with user authentication. You should pass this 2 parameters in http basic auth. It consists in a Base64 string with login and password separated by ":" A valid Authorization header is:
"Authorization: Basic base64({login}:{password})"
OR
an user_token defined in User Preference (See 'Remote access key') You should pass this parameter in 'Authorization' HTTP header. A valid Authorization header is:
"Authorization: user_token q56hqkniwot8wntb3z1qarka5atf365taaa2uyjrn"
Parameters: (query string)
get_full_session (default: false): Get the full session, useful if you want to login and access session data in one request.
Returns:
200 (OK) with the session_token string.
400 (Bad Request) with a message indicating an error in input parameter.
401 (UNAUTHORIZED)
Examples usage (CURL):
$ curl -X GET \
-H 'Content-Type: application/json' \
-H "Authorization: Basic Z2xwaTpnbHBp" \
-H "App-Token: f7g3csp8mgatg5ebc5elnazakw20i9fyev1qopya7" \
'http://path/to/glpi/apirest.php/initSession'
< 200 OK
< {
"session_token": "83af7e620c83a50a18d3eac2f6ed05a3ca0bea62"
}
$ curl -X GET \
-H 'Content-Type: application/json' \
-H "Authorization: user_token q56hqkniwot8wntb3z1qarka5atf365taaa2uyjrn" \
-H "App-Token: f7g3csp8mgatg5ebc5elnazakw20i9fyev1qopya7" \
'http://path/to/glpi/apirest.php/initSession?get_full_session=true'
< 200 OK
< {
"session_token": "83af7e620c83a50a18d3eac2f6ed05a3ca0bea62",
"session": {
'glpi_plugins': ...,
'glpicookietest': ...,
'glpicsrftokens': ...,
...
}
} |
J'ai donc créer un service dans GLPI :
src/Service/CallApiService.php
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| <?php
namespace App\Service;
use Symfony\Contracts\HttpClient\HttpClientInterface;
class CallApiService
{
private $client;
public function __construct(HttpClientInterface $client)
{
$this->client = $client;
}
public function getFranceData(): array
{
$response = $this->client->request(
'GET',
'https://serveurweb/glpi/apirest.php/initSession/',
// these values are automatically encoded before including them in the URL
['headers' => [
'Content-Type:' => 'application/json',
'Authorization' => 'UTILISATEUR MOTDEPASSE!!'
],]
);
dump($this->client);
try {
$response->toArray();
} catch (\Exception $e) {
dump($e->getMessage());
return array("message" => "Impossible requeteur API rest GLPI");
}
}
} |
j'ai donc le message retour d'erreur suivant :
Code:
"HTTP/1.1 400 Bad Request returned for "https://serveurweb/glpi/apirest.php/initSession/"."
avec l'entete suivante :
Code:
1 2 3 4 5 6 7 8
| https://serveurweb/glpi/apirest.php/initSession/
[▼
"headers" => [▼
"Content-Type:" => "application/json"
"Authorization" => "UTILISATEUR MOTDEPASSE!!"
]
] |
je pense que l'entete est correcte je ne vois pas se qui cloche dans l'appel
qu'en pensez vous svp ?