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 2022-12-13 23:58:18

Ederson Gonçalves
Member
Registered: 2022-12-06
Posts: 4

Help selecting User ID via API

Good night people,

I need some help selecting/capturing the user ID through an API.

The closest I got I'll describe below how I'm doing it.

ENDPOINT USED - http.../apirest.php/User/?range=3-4 **To return only 2 users and not the entire list.

RETURN:

[
    {
        "id": 5,
        "name": "normal",
        "password_last_update": null,
        "phone": null,
        "phone2": null,
        "mobile": null,
        "realname": null,
        "firstname": null,
        "locations_id": 0,
        "language": null,
        "use_mode": 0,
        "list_limit": null,
        "is_active": 0,
        "comment": null,
        "auths_id": 0,
        "authtype": 1,
        "last_login": "2020-09-28 21:45:50",
        "date_mod": "2020-10-18 15:15:52",
        "date_sync": null,
        "is_deleted": 0,
        "profiles_id": 0,
        "entities_id": 0,
        "usertitles_id": 0,
        "usercategories_id": 0,
        "date_format": null,
        "number_format": null,
        "names_format": null,
        "csv_delimiter": null,
        "is_ids_visible": null,
        "use_flat_dropdowntree": null,
        "show_jobs_at_login": null,
        "priority_1": null,
        "priority_2": null,
        "priority_3": null,
        "priority_4": null,
        "priority_5": null,
        "priority_6": null,
        "followup_private": null,
        "task_private": null,
        "default_requesttypes_id": null,
        "password_forget_token": null,
        "password_forget_token_date": null,
        "user_dn": null,
        "registration_number": null,
        "show_count_on_tabs": null,
        "refresh_views": null,
        "set_default_tech": null,
        "personal_token_date": null,
        "api_token_date": null,
        "cookie_token_date": null,
        "display_count_on_home": null,
        "notification_to_myself": null,
        "duedateok_color": null,
        "duedatewarning_color": null,
        "duedatecritical_color": null,
        "duedatewarning_less": null,
        "duedatecritical_less": null,
        "duedatewarning_unit": null,
        "duedatecritical_unit": null,
        "display_options": null,
        "is_deleted_ldap": 0,
        "pdffont": null,
        "picture": null,
        "begin_date": null,
        "end_date": null,
        "keep_devices_when_purging_item": null,
        "privatebookmarkorder": null,
        "backcreated": null,
        "task_state": null,
        "layout": null,
        "palette": null,
        "set_default_requester": null,
        "lock_autolock_mode": null,
        "lock_directunlock_notification": null,
        "date_creation": null,
        "highcontrast_css": null,
        "plannings": null,
        "sync_field": null,
        "groups_id": 0,
        "users_id_supervisor": 0,
        "timezone": null,
        "default_dashboard_central": null,
        "default_dashboard_assets": null,
        "default_dashboard_helpdesk": null,
        "default_dashboard_mini_ticket": null,
        "links": [
            {
                "rel": "Entity",
                "href": "http..../apirest.php/Entity/0"
            },
            {
                "rel": "Document_Item",
                "href": "http..../apirest.php/User/5/Document_Item/"
            }
        ]
    },
    {
        "id": 6,
        "name": "marcio.santos",
        "password_last_update": null,
        "phone": "",
        "phone2": "xxxxxxxx",
        "mobile": "xxxxxxxxx",
        "realname": "xxxxxxxxx",
        "firstname": "xxxxxxxxx",
        "locations_id": 178,
        "language": null,
        "use_mode": 0,
        "list_limit": 50,
        "is_active": 0,
        "comment": " | 512",
        "auths_id": 0,
        "authtype": 3,
        "last_login": "2022-04-08 12:48:00",
        "date_mod": "2022-10-07 16:16:34",
        "date_sync": "2022-04-08 12:48:00",
        "is_deleted": 0,
        "profiles_id": 4,
        "entities_id": 4,
        "usertitles_id": 1,
        "usercategories_id": 0,
        "date_format": null,
        "number_format": null,
        "names_format": null,
        "csv_delimiter": null,
        "is_ids_visible": null,
        "use_flat_dropdowntree": null,
        "show_jobs_at_login": null,
        "priority_1": null,
        "priority_2": null,
        "priority_3": null,
        "priority_4": null,
        "priority_5": null,
        "priority_6": null,
        "followup_private": 1,
        "task_private": null,
        "default_requesttypes_id": null,
        "password_forget_token": null,
        "password_forget_token_date": null,
        "user_dn": "CN=Marcio Santos,OU=Usuarios O365,OU=Users,OU=Employee Exceptions,OU=Information Technology,OU=6 - ADMINISTRATIVE AND FINANCIAL BOARD ,OU=XXXXXXXX,OU=PB,OU=BR,OU=IBQ,DC=IBQ,DC=local", "
        registration_number": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
        "show_count_on_tabs": null,
        "refresh_views": null,
        "set_default_tech": 0,
        "personal_token_date": "2020-06-05 02:36:48",
        "api_token_date": null,
        "cookie_token_date": "2020-09-14 18:28:38",
        "display_count_on_home": null,
        "notification_to_myself": null,
        "duedateok_color": null,
        "duedatewarning_color": null,
        "duedatecritical_color": null,
        "duedatewarning_less": null,
        "duedatecritical_less": null,
        "duedatewarning_unit": null,
        "duedatecritical_unit": null,
        "display_options": null,
        "is_deleted_ldap": 0,
        "pdffont": null,
        "picture": null,
        "begin_date": null,
        "end_date": null,
        "keep_devices_when_purging_item": null,
        "privatebookmarkorder": "[77,13,26,146]",
        "backcreated": null,
        "task_state": null,
        "layout": null,
        "palette": "dark",
        "set_default_requester": null,
        "lock_autolock_mode": null,
        "lock_directunlock_notification": null,
        "date_creation": "2020-05-25 21:06:04",
        "highcontrast_css": null,
        "plannings": "{\"filters\":{\"ChangeTask\":{\"color\":\"#E94A31\",\"display\":false,\"type\":\"event_filter\"},\"ProblemTask\":{\"color\":\"#5174F2\",\"display\":false,\"type\":\"event_filter\"},\"Reminder\":{\"color\":\"#51C9F2\",\"display\":false,\"type\":\"event_filter\"},\"TicketTask\":{\"color\":\"#FFCC29\",\"display\":false,\"type\":\"event_filter\"},\"ProjectTask\":{\"color\":\"#20C646\",\"display\":false,\"type\":\"event_filter\"}},\"plannings\":{\"user_6\":{\"color\":\"#FFEEC4\",\"display\":false,\"type\":\"user\"}}}",
        "sync_field": "marcio.atanasio",
        "groups_id": 0,
        "users_id_supervisor": 380,
        "timezone": null,
        "default_dashboard_central": null,
        "default_dashboard_assets": null,
        "default_dashboard_helpdesk": null,
        "default_dashboard_mini_ticket": null,
        "links": [
            {
                "rel": "Location",
                "href": "http..../apirest.php/Location/178"
            },
            {
                "rel": "Profile",
                "href": "http..../apirest.php/Profile/4"
            },
            {
                "rel": "Entity",
                "href": "http..../apirest.php/Entity/4"
            },
            {
                "rel": "UserTitle",
                "href": "http..../apirest.php/UserTitle/1"
            },
            {
                "rel": "User",
                "href": "http..../apirest.php/User/380"
            },
            {
                "rel": "Document_Item",
                "href": "http..../apirest.php/User/6/Document_Item/"
            }
        ]
    }
]

Below I am trying to capture/select only the ID, but it is returning an error (Error: Expected ',' or '}' after property value in JSON at position 4405), due to the "plannings" field in the list, I have already tried to remove the field "plannings" of the array but I didn't get it yet.

const json = '[ { "id": 5, "name": "normal", "password_last_update": null, "phone": null, "phone2": null, "mobile": null, "realname": null, "firstname": null, "locations_id": 0, "language": null, "use_mode": 0, "list_limit": null, "is_active": 0, "comment": null, "auths_id": 0, "authtype": 1, "last_login": "2020-09-28 21:45:50", "date_mod": "2020-10-18 15:15:52", "date_sync": null, "is_deleted": 0, "profiles_id": 0, "entities_id": 0, "usertitles_id": 0, "usercategories_id": 0, "date_format": null, "number_format": null, "names_format": null, "csv_delimiter": null, "is_ids_visible": null, "use_flat_dropdowntree": null, "show_jobs_at_login": null, "priority_1": null, "priority_2": null, "priority_3": null, "priority_4": null, "priority_5": null, "priority_6": null, "followup_private": null, "task_private": null, "default_requesttypes_id": null, "password_forget_token": null, "password_forget_token_date": null, "user_dn": null, "registration_number": null, "show_count_on_tabs": null, "refresh_views": null, "set_default_tech": null, "personal_token_date": null, "api_token_date": null, "cookie_token_date": null, "display_count_on_home": null, "notification_to_myself": null, "duedateok_color": null, "duedatewarning_color": null, "duedatecritical_color": null, "duedatewarning_less": null, "duedatecritical_less": null, "duedatewarning_unit": null, "duedatecritical_unit": null, "display_options": null, "is_deleted_ldap": 0, "pdffont": null, "picture": null, "begin_date": null, "end_date": null, "keep_devices_when_purging_item": null, "privatebookmarkorder": null, "backcreated": null, "task_state": null, "layout": null, "palette": null, "set_default_requester": null, "lock_autolock_mode": null, "lock_directunlock_notification": null, "date_creation": null, "highcontrast_css": null, "plannings": null, "sync_field": null, "groups_id": 0, "users_id_supervisor": 0, "timezone": null, "default_dashboard_central": null, "default_dashboard_assets": null, "default_dashboard_helpdesk": null, "default_dashboard_mini_ticket": null, "links": [ { "rel": "Entity", "href": "http..../apirest.php/Entity/0" }, { "rel": "Document_Item", "href": "http..../apirest.php/User/5/Document_Item/" } ] }, { "id": 6, "name": "marcio.santos", "password_last_update": null, "phone": "", "phone2": "xxxxxxxx", "mobile": "xxxxxxxxx", "realname": "xxxxxxxxx", "firstname": "xxxxxxxxx", "locations_id": 178, "language": null, "use_mode": 0, "list_limit": 50, "is_active": 0, "comment": " | 512", "auths_id": 0, "authtype": 3, "last_login": "2022-04-08 12:48:00", "date_mod": "2022-10-07 16:16:34", "date_sync": "2022-04-08 12:48:00", "is_deleted": 0, "profiles_id": 4, "entities_id": 4, "usertitles_id": 1, "usercategories_id": 0, "date_format": null, "number_format": null, "names_format": null, "csv_delimiter": null, "is_ids_visible": null, "use_flat_dropdowntree": null, "show_jobs_at_login": null, "priority_1": null, "priority_2": null, "priority_3": null, "priority_4": null, "priority_5": null, "priority_6": null, "followup_private": 1, "task_private": null, "default_requesttypes_id": null, "password_forget_token": null, "password_forget_token_date": null, "user_dn": "CN=Marcio Santos,OU=Usuarios O365,OU=Usuários,OU=Colaborador Exceções,OU=Tecnologia da Informação,OU=6 - DIRETORIA ADMINISTRATIVA E FINANCEIRA,OU=XXXXXXXX,OU=PB,OU=BR,OU=IBQ,DC=IBQ,DC=local", "registration_number": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", "show_count_on_tabs": null, "refresh_views": null, "set_default_tech": 0, "personal_token_date": "2020-06-05 02:36:48", "api_token_date": null, "cookie_token_date": "2020-09-14 18:28:38", "display_count_on_home": null, "notification_to_myself": null, "duedateok_color": null, "duedatewarning_color": null, "duedatecritical_color": null, "duedatewarning_less": null, "duedatecritical_less": null, "duedatewarning_unit": null, "duedatecritical_unit": null, "display_options": null, "is_deleted_ldap": 0, "pdffont": null, "picture": null, "begin_date": null, "end_date": null, "keep_devices_when_purging_item": null, "privatebookmarkorder": "[77,13,26,146]", "backcreated": null, "task_state": null, "layout": null, "palette": "dark", "set_default_requester": null, "lock_autolock_mode": null, "lock_directunlock_notification": null, "date_creation": "2020-05-25 21:06:04", "highcontrast_css": null, "plannings": "{\"filters\":{\"ChangeTask\":{\"color\":\"#E94A31\",\"display\":false,\"type\":\"event_filter\"},\"ProblemTask\":{\"color\":\"#5174F2\",\"display\":false,\"type\":\"event_filter\"},\"Reminder\":{\"color\":\"#51C9F2\",\"display\":false,\"type\":\"event_filter\"},\"TicketTask\":{\"color\":\"#FFCC29\",\"display\":false,\"type\":\"event_filter\"},\"ProjectTask\":{\"color\":\"#20C646\",\"display\":false,\"type\":\"event_filter\"}},\"plannings\":{\"user_6\":{\"color\":\"#FFEEC4\",\"display\":false,\"type\":\"user\"}}}", "sync_field": "marcio.atanasio", "groups_id": 0, "users_id_supervisor": 380, "timezone": null, "default_dashboard_central": null, "default_dashboard_assets": null, "default_dashboard_helpdesk": null, "default_dashboard_mini_ticket": null, "links": [ { "rel": "Location", "href": "http..../apirest.php/Location/178" }, { "rel": "Profile", "href": "http..../apirest.php/Profile/4" }, { "rel": "Entity", "href": "http..../apirest.php/Entity/4" }, { "rel": "UserTitle", "href": "http..../apirest.php/UserTitle/1" }, { "rel": "User", "href": "http..../apirest.php/User/380" }, { "rel": "Document_Item", "href": "http..../apirest.php/User/6/Document_Item/" } ] } ]';

const obj = JSON.parse(json);

console.log(obj.id);

Can anyone help me or know how to tell me another way to capture/select the user ID through login(user)?

Offline

#2 2022-12-14 11:01:33

Kaya84
Member
Registered: 2019-06-13
Posts: 217

Re: Help selecting User ID via API

The json seems correctly formatted.

Which programming language are you using ?

Offline

#3 2022-12-14 11:21:42

LaDenrée
HELPER
Registered: 2012-11-19
Posts: 6,287

Re: Help selecting User ID via API

what about  json[0][id]  ?

or use search endpoint :

$url=$api_url . "/search/User?forcedisplay[0]=2&forcedisplay[1]=5&criteria[1][field]=1&criteria[1][searchtype]=contains&criteria[1][value]=" . $username;

see also here :
https://forum.glpi-project.org/viewtopic.php?id=278783


Trouver la panne avant de réparer...
GLPI10.0.16 (ubuntu 22.04 PHP8.1  Mariadb10.6 ) plugins : comportements 2.7.3 reports 1.16.0 formcreator 2.13.9, datainjection 2.13.5 fields 1.21.9

Offline

#4 2022-12-14 12:59:31

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

Re: Help selecting User ID via API

Can you provide some more details about the use case? A new API is in development so I want to check if this case can easily be covered in the new API too.

https://github.com/glpi-project/glpi/pull/12221


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

#5 2022-12-14 20:25:30

Ederson Gonçalves
Member
Registered: 2022-12-06
Posts: 4

Re: Help selecting User ID via API

LaDenrée wrote:

what about  json[0][id]  ?

or use search endpoint :

$url=$api_url . "/search/User?forcedisplay[0]=2&forcedisplay[1]=5&criteria[1][field]=1&criteria[1][searchtype]=contains&criteria[1][value]=" . $username;

see also here :

Hi!

First of all thank you very much! It worked and I already managed to do the treatment to get the ID only.

But I would like to know, is there a way to include this search to return the name as well?

Because it is returning to me like this:

{
    "totalcount": 1,
    "count": 1,
    "sort": "1",
    "order": "ASC",
    "data": [
        {
            "1": "marcio.santos",
            "2": 6,
            "5": "marcio.santos@xxxxxx.com"
        }
    ],
    "content-range": "0-0/1"
}

Thank you very much already!

Offline

#6 2022-12-15 00:58:46

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

Re: Help selecting User ID via API

Here are some of the available search options for Users:
Selection-122.png

You can force them to be included in the API result by adding "forcedisplay[]" query parameters to the URL where the value is a search option ID.
For example, to add the last name (option 34) and first name (option 9):
$url=$api_url . "/search/User?forcedisplay[0]=2&forcedisplay[1]=5&forcedisplay[2]=34&forcedisplay[3]=9&criteria[1][field]=1&criteria[1][searchtype]=contains&criteria[1][value]=" . $username;


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