pythonpython-3.xgoogle-contacts-apigoogle-people-api

Search google contact using People Api v3?


I'm trying to use google People API to search a person by her/his name field. Here is the code sample:

service = build('people', 'v1', credentials=creds)
request = service.people().searchContacts(pageSize=10, query="A", readMask="names")
print(request.body) # results in None, but there is a lot of contacts in my list starting from "A". 

I used the following links:

https://developers.google.com/people/v1/contacts#python

https://googleapis.github.io/google-api-python-client/docs/dyn/people_v1.people.html#get

https://developers.google.com/people/quickstart/python

and the SCOPE is https://www.googleapis.com/auth/contacts.readonly.

I need a way to return a list of contacts using name mask (for ex, person with the name "Foo bar" should be found using "f", "F", "foo", and so on).


Solution

  • Answer:

    You aren't executing your request, only referencing it.

    More Information:

    In Python, a method without a defined return will always return None.

    As you are not making the request, no return value is being obtained and so you are seeing none displayed.

    Code Fix:

    You need to execute the request like so:

    service.people().searchContacts(pageSize=10, query="A", readMask="names").execute()
    

    Also, the response object has no property body, so you will need to use

    print(request.results)
    

    to view the response text.