google places api - Unknown field for SearchTextRequest: fields - Stack Overflow

admin2025-04-16  5

I get the error Unknown field for SearchTextRequest: included_fields when trying make a request with client library in Google Places API. There is instruction about how set fieldmask in http requests () but I couldn't find instructions for client library.

async def sample_get_place():
    # Create a client
    client = places_v1.PlacesAsyncClient(client_options={'api_key':api_key.api_key})

    # Initialize request argument(s)
    request = places_v1.SearchTextRequest(
        text_query="restaurants in New York",  # Updated query for places in a city
        fields=["places.display_name"]      
    )

    # Make the request
    response = await client.search_text(request=request)
    # Make the request
    # response = await client.get_place(request=request)

I get the error Unknown field for SearchTextRequest: included_fields when trying make a request with client library in Google Places API. There is instruction about how set fieldmask in http requests (https://developers.google.com/maps/documentation/places/web-service/text-search#fieldmask) but I couldn't find instructions for client library.

async def sample_get_place():
    # Create a client
    client = places_v1.PlacesAsyncClient(client_options={'api_key':api_key.api_key})

    # Initialize request argument(s)
    request = places_v1.SearchTextRequest(
        text_query="restaurants in New York",  # Updated query for places in a city
        fields=["places.display_name"]      
    )

    # Make the request
    response = await client.search_text(request=request)
    # Make the request
    # response = await client.get_place(request=request)
Share Improve this question asked Feb 2 at 9:11 Fuad AkFuad Ak 1588 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

This is because fields is not part of the request body, but instead must go in the request headers. As explained in https://googleapis.dev/python/places/latest/places_v1/places.html

Note: 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. See: https://developers.google.com/maps/documentation/places/web-service/choose-fields

To do this, add metadata when sending the request:

# Make the request with headers.
response = await client.search_text(
    request=request,
    metadata=[("x-goog-fieldmask","places.id,places.display_name")])

Hopefully this should be easier in the future, if/when https://github.com/googleapis/gapic-generator-python/issues/2054 is implemented.

转载请注明原文地址:http://anycun.com/QandA/1744806016a87873.html