unistra / python-glpi-api

Python module for interacting with GLPI using the API.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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'))