unistra / python-glpi-api

Python module for interacting with GLPI using the API.
GNU General Public License v3.0
18 stars 10 forks source link

Alternative auth with user_token? #19

Closed blippercop closed 7 months ago

blippercop commented 11 months ago

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

fmenabe commented 10 months ago

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'))
blippercop commented 10 months ago

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: @.***>