Alternative auth with user_token?
blippercop opened this issue · comments
Having this code:
import requests
init_headers = {
'User-Agent': 'SOMETHING',
'Authorization': 'user_token [https://mydomain.com/front/preference.php API-TOKEN HERE]'
}
init_response = requests.get('https://mydomain.com//apirest.php/initSession/', headers=init_headers)
session_token = init_response.json()['session_token']
post_headers = {
'User-Agent': 'SOMETHING',
'Content-Type': 'application/json',
'Authorization': 'user_token [https://mydomain.com/front/preference.php API-TOKEN HERE]',
'Session-Token': session_token
}
# Define the query parameters to get all open tickets
query_params = {
'itemtype': 'Ticket',
}
data_response = requests.get('https://mydomain.comapirest.php/search/Ticket/', headers=post_headers,
params=query_params)
print(data_response.text)
I am able to get the tickets by the API. Is it possible to add the user_token method to the authentication methods?
Thanks
Hi, sorry for the late answer.
It should already be possible without modifying the library. By default, requests
removes headers that have None as value (pinpointed to https://github.com/psf/requests/blob/main/src/requests/sessions.py#L82). So, you just need to set the APP_TOKEN to None and the header will not be included.
The following code works for me:
import glpi_api
URL = 'http://127.0.0.1:8080/apirest.php'
APPTOKEN = None
USERTOKEN = 'FIXME'
with glpi_api.connect(URL, APPTOKEN, USERTOKEN) as glpi:
print(glpi.search('Ticket'))
Aha! Setting it to None. I tried omitting it which did not work as it was required. So I assumed it is not possible. Thanks, that helped! It works now! :)Am 26.10.2023 um 14:09 schrieb François Ménabé ***@***.***>:
Hi, sorry for the late answer.
It should already be possible without modifying the library. By default, requests removes headers that have None as value (pinpointed to https://github.com/psf/requests/blob/main/src/requests/sessions.py#L82). So, you just need to set the APP_TOKEN to None and the header will not be included.
The following code works for me:
import glpi_api
URL = 'http://127.0.0.1:8080/apirest.php'
APPTOKEN = None
USERTOKEN = 'FIXME'
with glpi_api.connect(URL, APPTOKEN, USERTOKEN) as glpi:
print(glpi.search('Ticket'))
—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you authored the thread.Message ID: ***@***.***>