1 min read

Add fieldmask on the new Google Places API - Python Client

Add fieldmask on the new Google Places API - Python Client
Photo by Matteo Catanese / Unsplash

Hello ! 👋

If you used the Google Cloud Client Libraries, Google-maps-places and want to make a request with text search (new) (or other function in this lib) you probably get the following error :

google.api_core.exceptions.InvalidArgument: 400 FieldMask is a required parameter...

The doc is not very explicit on this point, as you can see…

Every request (except for Autocomplete requests) requires a field mask set outside of the request proto (all/*, is not assumed). The field mask can be set via the HTTP header X-Goog-FieldMask.

As we don't have any parameter named "fieldsMask" or "Header" in the documentation, How do we supply it to our function ?

Simply add x-goog-fieldmask on the metadata parameter of your function with your requested fields attach to it :

# Initialize request
request = places_v1.SearchTextRequest(text_query="Google Paris")

field_mask = "places.id,places.name"  # Free request
# Make the request
response = client.search_text(
    request=request, metadata=[("x-goog-fieldmask", field_mask)]
Note : this is working with : search_text, search_nearby, get_place, get_photo_media, but not with autocomplete_places as it does not support it

Source: https://github.com/googleapis/google-cloud-python/issues/12083