Search code examples
google-admin-sdkgoogle-workspacegoogle-api-python-client

Is there a way to search users by externalid using the googleapis/google-api-python-client library?


I am writing a python application that uses the Google Admin SDK to search for users in the user directory. I need to search users by the External ID or the Employee ID, it doesn't seem to be implemented in the python library (https://github.com/googleapis/google-api-python-client#installation), but there is support from google as it says in their official docs, see (https://developers.google.com/admin-sdk/directory/v1/guides/search-users#examples)

I would really appreciate it if someone can help point things out.


Solution

  • Answer

    It is possible to search for user by the external Id using the query parameter query.

    Step by step

    1. Check the Python Quickstart
    2. Use the method users.list
    3. Define the customer. You can use my_customer
    4. Define your query. Example: query = 'externalId:someValue'
    5. Get the users that match the result of the list

    Code

    results = service.users().list( customer='my_customer',
                                    query='externalId:someValue',
                                    orderBy='email'
                                    ).execute()
    users = results.get('users', [])
    

    References