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 2023-01-31 12:31:43

headquaker
Member
Registered: 2015-03-23
Posts: 40

API access from computer is working but not from server

Hello,

I've created a powershell script that could create tickets in glpi thru the API.

It's working great from a computer but the same script from a server (Windows 2016) does not work.

I ve allowed the server ip to connect to glpi, the tokens are correct, the FW is correct, the server can join glpi (i can display the glpi documentation).

In logs I got that error : client used wrong authentication scheme: (null) for /apirest.php/initSession (needed NTLM)

So i don't understant, any help ?

More information that is really strange :

From the computer, I can access to the server using those two URLs :
http://hostname/portail-glpi-prep-v10/front/central.php
http://dnsalias/front/central.php

But from the server, only the url with the alias is working.

Script :

# GLPI REST API CONFIG :
$AppURL =     "http://dnsalias/apirest.php" (SAME ISSUE WITH THE OTHER URL)
$UserToken =  "USERTOKEN"
$AppToken =   "APITOKEN"
$createUrl = "http://dnsalias/apirest.php/Ticket"

I've tried those to line :
# session creation
$SessionToken = Invoke-RestMethod "$AppURL/initSession" -Method Get -Headers @{"Content-Type" = "application/json";"Authorization" = "user_token $UserToken";"App-Token"=$AppToken}
$SessionToken = Invoke-RestMethod "$AppURL/initSession" -Method Get -Headers @{"Content-Type" = "application/json";"Authorization" = "user_token $UserToken";"App-Token"=$AppToken;"Authentication" = "NTLM"}


Invoke-RestMethod :
401 Unauthorized
Unauthorized
This server could not verify that you
are authorized to access the document
requested.  Either you supplied the wrong
credentials (e.g., bad password), or your
browser doesn't understand how to supply
the credentials required.

EDIT : and YES I've changed the URL in the API Settings, and the API is enabled because it works from a computer.

Last edited by headquaker (2023-01-31 12:36:37)

Offline

#2 2023-02-01 11:57:35

headquaker
Member
Registered: 2015-03-23
Posts: 40

Re: API access from computer is working but not from server

Hello, anyhelp ?

Offline

#3 2023-02-01 17:23:29

headquaker
Member
Registered: 2015-03-23
Posts: 40

Re: API access from computer is working but not from server

I am answering to myself but here the solution for you... like usual.
I don't know why, but from the serveur I should use NTLM and have to pass the parameters thru the URL and not the header.

cls
# GLPI API REST Endpoint URL
$GLPI_API_URL = "http://tupeuxpastest/apirest.php"

# GLPI API User Token
$GLPI_API_USER_TOKEN = "XXXXXXXXXX"

# GLPI API Application Token
$GLPI_API_APP_TOKEN = "YYYYYYYYYY"

# Request headers for GLPI API requests
$Headers = @{
  "Content-Type" = "application/json"
}

$URLSESSION =  $GLPI_API_URL + "/initSession?user_token=" + $GLPI_API_USER_TOKEN + "&app_token=" + $GLPI_API_APP_TOKEN

# Connect to GLPI API using the specified endpoint URL and tokens
$SessionToken = Invoke-WebRequest -Method Get -Uri $URLSESSION  -Headers $Headers -UseDefaultCredentials

# Session Token
$SessionToken = $SessionToken.Content | ConvertFrom-Json
$SessionToken = $SessionToken.session_token

# Update request headers with session token
$Headers["Session-Token"] = $SessionToken
$data = @{
    "input" = @(
        @{
            "content" = "Ticket description"
            "name" = "Depart Salarie"
            "_groups_id_requester" = "1"
            "priority" = "4"
            "urgency" = "2"
            "status" = "1"
            "impact" = "3"
        }
    )
}
$json = $data | ConvertTo-Json

$URLSESSION =  $GLPI_API_URL + "/Ticket?user_token=" + $GLPI_API_USER_TOKEN + "&app_token=" + $GLPI_API_APP_TOKEN

# Connect to GLPI API using the specified endpoint URL and tokens
$SessionToken = Invoke-WebRequest -Method Post -Uri $URLSESSION  -Headers $Headers -UseDefaultCredentials -Body $json

If I don't need anything, I'll ask you smile

Last edited by headquaker (2023-02-01 17:41:00)

Offline

Board footer

Powered by FluxBB