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-05-21 18:43:30

Uplifter
Member
Registered: 2023-05-21
Posts: 6

Search user ID with Email - Python

Hey there,

I have code that opens a ticket from Slack using Python.

For the life of me, I can't seem to figure out how to pull the user ID from the database by email, since this is the (logical) parameter I requested from Slack.

I am trying to assign a ticket requester (_users_id_requester) with this method but it seems the email is not doing anything, so I need an ID.

While running the following code I get an error:

Error: 400 ["ERROR_RESOURCE_MISSING","missing resource;

How can this be achieved?

# Set the GLPI API URL, API key, and email to search
api_url = 'https://myGLPIurl/apirest.php'
api_key = API_KEY
email = 'me@me.com'

def search_user_id_by_email(api_url, api_key, email):
    headers = {
        'Content-Type': 'application/json',
        'App-Token': API_KEY,
        'Session-Token': session_token,
    }

    params = {
        'itemtype': 'User',
        'criteria[0][field]': '1',
        'criteria[0][searchtype]': 'contains',
        'criteria[0][value]': email
    }

    response = requests.get(api_url + '/search', headers=headers, params=params)
    print(api_url + '/search', headers, params)

    if response.status_code == 200:
        data = response.json()
        if 'totalcount' in data and data['totalcount'] > 0:
            user_id = data['data'][0]['id']
            return user_id
    else:
        print('Error:', response.status_code, response.text)

    return None

Last edited by Uplifter (2023-05-21 18:46:39)

Offline

#2 2023-05-22 00:57:23

cconard96
Moderator
Registered: 2018-07-31
Posts: 2,418
Website

Re: Search user ID with Email - Python

Hello,

Your search parameters do not seem correct.
If I do a search for "Emails" contains "me@me.com" in the web UI, I can see that the following URL query parameters are generated for the search:
'criteria[0][link]=AND&criteria[0][field]=5&criteria[0][searchtype]=contains&criteria[0][value]=me@me.com&itemtype=User'

You have the "field" value for the first criteria set to 1 which is the search option ID for the item ID while 5 is the search option ID for the emails.


GLPI Collaborator and Plugin Developer.
My non-English comments are automated translations. Sorry for any confusion that causes.
Mes commentaires non anglais sont des traductions automatiques. Désolé pour toute confusion qui cause.
Mis comentarios que no están en inglés son traducciones automáticas. Perdón por cualquier confusión que cause.

Offline

Board footer

Powered by FluxBB